-
Notifications
You must be signed in to change notification settings - Fork 38
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Macket messenger #130
base: master
Are you sure you want to change the base?
Macket messenger #130
Changes from 11 commits
6fd4bdc
08ff89c
99afbf5
b2b1083
994359d
cdc8609
ea09c19
17d957a
9cb2165
caed98f
ac6a11a
b5dc44e
984655b
9f08efb
e494ddb
f74c1d3
d80dad5
ed1c774
719e233
d24b67f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Макеев Иван Алексеевич, СА-11 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package track.msgtest.messenger.mymessenger; | ||
|
||
import java.util.Iterator; | ||
import java.util.LinkedList; | ||
|
||
public class BlockingQueue<E> { | ||
|
||
private static final Object monitor = new Object(); | ||
|
||
private LinkedList<E> list = new LinkedList<>(); | ||
|
||
public synchronized void put(E elem) throws InterruptedException { | ||
if (this.isEmpty()) { | ||
notifyAll(); | ||
} | ||
list.addLast(elem); | ||
} | ||
|
||
public synchronized E take() throws InterruptedException { | ||
while (this.isEmpty()) { | ||
try { | ||
wait(); | ||
} catch (InterruptedException e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
return list.removeFirst(); | ||
} | ||
|
||
public boolean isEmpty() { | ||
return list.size() == 0; | ||
} | ||
|
||
public int getSize() { | ||
return list.size(); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
StringBuilder stringBuilder = new StringBuilder(); | ||
Iterator<E> iterator = list.iterator(); | ||
while (iterator.hasNext()) { | ||
stringBuilder.append(iterator.next()); | ||
} | ||
return new String(stringBuilder); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package track.msgtest.messenger.mymessenger; | ||
|
||
/** | ||
* Created by ivan on 18.04.17. | ||
*/ | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.io.OutputStream; | ||
import java.net.Socket; | ||
import java.net.SocketException; | ||
import java.util.Scanner; | ||
|
||
public class MyClient { | ||
private static final int BUFSIZE = 10; | ||
|
||
public static void main(String[] args) throws IOException { | ||
if (args.length < 1 || args.length > 2) { | ||
throw new IllegalArgumentException("Parameters: <Server> <word> [<Port>]"); | ||
} | ||
|
||
String server = args[0]; | ||
|
||
int servPort = (args.length == 2) ? Integer.parseInt(args[1]) : 7; | ||
|
||
Socket socket = new Socket(server, servPort); | ||
InputStream in = socket.getInputStream(); | ||
OutputStream out = socket.getOutputStream(); | ||
|
||
Thread outThread = new Thread() { | ||
@Override | ||
public void run() { | ||
while (true) { | ||
// Считываем сообщение с консоли | ||
Scanner scanner = new Scanner(System.in); | ||
byte[] data = scanner.nextLine().getBytes(); | ||
try { | ||
out.write(data); | ||
} catch (IOException ex) { | ||
ex.printStackTrace(); | ||
} | ||
} | ||
} | ||
}; | ||
|
||
outThread.start(); | ||
|
||
byte[] data = new byte[1024]; | ||
int bytesRcvd; | ||
|
||
while ((bytesRcvd = in.read(data)) != -1) { | ||
System.out.println("Received:" + new String(data).trim()); | ||
data = new byte[1024]; | ||
} | ||
//socket.close(); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package track.msgtest.messenger.mymessenger; | ||
|
||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.io.OutputStream; | ||
import java.net.ServerSocket; | ||
import java.net.Socket; | ||
import java.net.SocketAddress; | ||
import java.util.Iterator; | ||
import java.util.LinkedList; | ||
|
||
public class MyServer { | ||
private static final int BUFSIZE = 100; | ||
private static LinkedList<OutputStream> outs = new LinkedList<>(); | ||
private static BlockingQueue<byte[]> recieveBufs = new BlockingQueue<>(); | ||
|
||
public static void main(String[] args) throws IOException { | ||
if (args.length != 1) { | ||
throw new IllegalArgumentException("Parameter: <Port>"); | ||
} | ||
LinkedList<Integer> list = new LinkedList<>(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. название ни о чем |
||
|
||
int servPort = Integer.parseInt(args[0]); | ||
|
||
ServerSocket serverSocket = new ServerSocket(servPort); | ||
|
||
while (true) { | ||
Socket clntSock = serverSocket.accept(); | ||
outs.addLast(clntSock.getOutputStream()); | ||
|
||
Thread inThread = new Thread() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Эти треды стоит где-то сохранять, чтобы потом можно было ими управлять. |
||
@Override | ||
public void run() { | ||
try { | ||
int recvMsgSize; | ||
byte[] recieveBuf = new byte[BUFSIZE]; | ||
|
||
SocketAddress clientAddress = clntSock.getRemoteSocketAddress(); | ||
System.out.println("Handling client at " + clientAddress); | ||
|
||
InputStream in = clntSock.getInputStream(); | ||
|
||
while ((recvMsgSize = in.read(recieveBuf)) != -1) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. У вас работа с клиентом уже вынесена в поток. Вы же на каждое чтение заводите еще один новый поток на запись. Либо оставьте запись в основном потоке либо заведите отдельный, но 1 и передавайте ткда данные через очередь |
||
OutThread outThread = new OutThread(recvMsgSize, recieveBuf, outs); | ||
outThread.start(); | ||
} | ||
clntSock.close(); | ||
} catch (IOException ex) { | ||
ex.printStackTrace(); | ||
} | ||
} | ||
}; | ||
|
||
inThread.start(); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package track.msgtest.messenger.mymessenger; | ||
|
||
import java.io.IOException; | ||
import java.io.OutputStream; | ||
import java.util.LinkedList; | ||
|
||
/** | ||
* Created by ivan on 22.04.17. | ||
*/ | ||
public class OutThread extends Thread { | ||
|
||
int recvMsgSize; | ||
byte[] recieveBuf; | ||
LinkedList<OutputStream> outs; | ||
|
||
public OutThread( int recvMsgSize, byte[] recieveBuf, LinkedList<OutputStream> outs ) { | ||
this.recvMsgSize = recvMsgSize; | ||
this.recieveBuf = recieveBuf; | ||
this.outs = outs; | ||
} | ||
|
||
@Override | ||
public void run() { | ||
try { | ||
for (OutputStream out : outs) { | ||
out.write(recieveBuf, 0, recvMsgSize ); | ||
out.flush(); | ||
} | ||
} catch (IOException ex) { | ||
ex.printStackTrace(); | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
не жмитесь, сделайте буфер 2048 чтобы не иметь проблем с размером сообщения