multithreading - chat in java not synchronized (sockets, threads) -
i'm trying figure out way instance of server negotiate between 2 clients creating chat thread between them.
i created project, , "almost" works... seems there buffer of synch problem.
when writing line in 1 side (i.e client#1), doesn't pass other side (i.e client#2), after client#2 trys pass line too.
i know there might better ways implement this, i'd understand what's wrong my code.
your great!
the code:
server
import java.io.*; import java.net.*; public class server { public static void main(string[] args) { int id = 1; system.out.println(); system.out.println("server"); try { serversocket serversocket = new serversocket(4321); while (true) { socket client1socket = serversocket.accept(); socket client2socket = serversocket.accept(); system.out.println("clients connected ports: \n" + client1socket.getport() + ", " + client2socket.getport()); thread client1thread = new serverthread(client1socket, client2socket, id); client1thread.start(); id++; thread client2thread = new serverthread(client2socket, client1socket, id); client2thread.start(); id++; } } catch (ioexception ioe) { ioe.printstacktrace(); } } }
server thread
import java.io.*; import java.net.*; import java.util.*; public class serverthread extends thread { socket sourcesocket; socket destsocket; int id; public serverthread(socket src, socket dst, int n) { sourcesocket = src; destsocket = dst; id = n; } public void run() { try { scanner clientinput = new scanner(sourcesocket.getinputstream()); printstream destoutput = new printstream(destsocket.getoutputstream()); destoutput.println("you chatting client " + id); boolean more = true; while (more) { string input = clientinput.nextline(); destoutput.println(input); if (input.equals("q")) { more = false; } } sourcesocket.close(); destsocket.close(); } catch (ioexception ex) { ex.printstacktrace(); } } }
client
import java.io.*; import java.net.*; import java.util.*; public class client { public static void main(string[] args) { system.out.println(); system.out.println("client"); try { socket clientsocket = new socket("localhost", 4321); system.out.println("connection established"); scanner input = new scanner(clientsocket.getinputstream()); printstream output = new printstream(clientsocket.getoutputstream()); scanner in = new scanner(system.in); system.out.println(input.nextline()); boolean more = true; while (more) { string text = in.nextline(); output.println(text); string nextinput = input.nextline(); if (nextinput == null) { more = false; } else { system.out.println(nextinput); } } } catch (ioexception ex) { ex.printstacktrace(); } } }
in client code, line string text = in.nextline();
block thread. means if never type in in client, can not receive anything. solution put message receiving code in thread. such as:
thread thread = new thread(new runnable() { @override public void run() { // todo auto-generated method stub while (!thread.interrupted()) { system.out.println(input.nextline()); } } }); thread.start(); while (true) { string text = in.nextline(); output.println(text); // string nextinput = input.nextline(); ...................... }
Comments
Post a Comment