100% found this document useful (1 vote)
193 views

Java TCP Chat - Multiple Client - Java Developers Zone

This document describes a Java TCP chat program that allows for multiple clients to connect to a server and chat. The server runs first and waits for clients to connect. When a client connects, a new thread is started to handle communication with that client. The server and client programs use sockets, input/output streams, and readers/writers to exchange messages. The client runs in a loop, allowing the user to enter messages and receive responses from the server, until the user enters "EXIT" to close the connection.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
193 views

Java TCP Chat - Multiple Client - Java Developers Zone

This document describes a Java TCP chat program that allows for multiple clients to connect to a server and chat. The server runs first and waits for clients to connect. When a client connects, a new thread is started to handle communication with that client. The server and client programs use sockets, input/output streams, and readers/writers to exchange messages. The client runs in a loop, allowing the user to enter messages and receive responses from the server, until the user enters "EXIT" to close the connection.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

3/10/2017 JavaTCPChatMultipleClient|JavaDevelopersZone

JavaDevelopersZone

JavaDeveloper'snotes

JavaTCPChatMultipleClient

i
14Votes

INFO:Theseprogramsworkasfollowing

servermustberunningrst
anynumberofclientscanberunthen
serverrandomlytakerequestfromanyclientandrespondtoit
torespondclientservermaintainsthatmanynumberofthreads
eachthreadsharethesameobject,whichobjectsmethodwill
respondtoclient,thussynchronizationisachieved
clientprogramrunsuntilcliententersEXIT

SERVERPROGRAM:

1 publicclassTCPMultiServer{
2
3 publicstaticvoidmain(Stringargv[])throwsException{
4
5 ServerSocketwelcomeSocket=newServerSocket(6789);
6
7 Responderh=newResponder();
8 //serverrunsforinfinitetimeand
9 //waitforclientstoconnect
10 while(true){
11 //waiting..
12 SocketconnectionSocket=welcomeSocket.accept();

13
https://javadeveloperszone.wordpress.com/2013/04/20/javatcpchatmultipleclient/ 1/5
3/10/2017 JavaTCPChatMultipleClient|JavaDevelopersZone

13
14 //onconnectionestablishmentstartanewthreadforeachclient
15 //eachthreadsharesacommonresponderobject
16 //whichwillbeusedtorespondeveryclientrequest
17 //needtosynchronizemethodofcommonobjectnottohaveunexpectedbehaviour
18 Threadt=newThread(newMyServer(h,connectionSocket));
19
20 //startthread
21 t.start();
22
23 }
24 }
25 }
26
27 classMyServerimplementsRunnable{
28
29 Responderh;
30 SocketconnectionSocket;
31
32 publicMyServer(Responderh,SocketconnectionSocket){
33 this.h=h;
34 this.connectionSocket=connectionSocket;
35 }
36
37 @Override
38 publicvoidrun(){
39
40 while(h.responderMethod(connectionSocket)){
41 try{
42 //onceanconversationwithoneclientdone,
43 //givechancetootherthreads
44 //somakethisthreadsleep
45 Thread.sleep(5000);
46 }catch(InterruptedExceptionex){
47 ex.printStackTrace();
48 }
49 }
50
51 try{
52 connectionSocket.close();
53 }catch(IOExceptionex){
54 Logger.getLogger(MyServer.class.getName()).log(Level.SEVERE,
55 }
56
57 }
58
59 }
60
61 classResponder{
62
63 StringserverSentence;
64 BufferedReaderbr=newBufferedReader(newInputStreamReader(System.in));
65
66 //onclientprocessterminationor
67 //clientsendsEXITthentoreturnfalsetocloseconnection
68 //elsereturntruetokeepconnectionalive
69 //andcontinueconversation
70 synchronizedpublicbooleanresponderMethod(SocketconnectionSocket){
https://javadeveloperszone.wordpress.com/2013/04/20/javatcpchatmultipleclient/ 2/5
3/10/2017 JavaTCPChatMultipleClient|JavaDevelopersZone

70 synchronizedpublicbooleanresponderMethod(SocketconnectionSocket){
71 try{
72
73 BufferedReaderinFromClient=
74 newBufferedReader(
75 newInputStreamReader(
76 connectionSocket.getInputStream()));
77
78 DataOutputStreamoutToClient=
79 newDataOutputStream(
80 connectionSocket.getOutputStream());
81
82 StringclientSentence=inFromClient.readLine();
83
84 //ifclientprocessterminatesitgetnull,socloseconnection
85 if(clientSentence==null||clientSentence.equals(
86 returnfalse;
87 }
88
89 if(clientSentence!=null){
90 System.out.println("client:"+clientSentence);
91 }
92 serverSentence=br.readLine()+"\n";
93
94 outToClient.writeBytes(serverSentence);
95
96 returntrue;
97
98 }catch(SocketExceptione){
99 System.out.println("Disconnected");
100 returnfalse;
101 }catch(Exceptione){
102 e.printStackTrace();
103 returnfalse;
104 }
105 }
106 }

CLIENTPROGRAM:

1 publicclassTCPMultiClient{
https://javadeveloperszone.wordpress.com/2013/04/20/javatcpchatmultipleclient/ 3/5
3/10/2017 JavaTCPChatMultipleClient|JavaDevelopersZone

1 publicclassTCPMultiClient{
2
3 publicstaticvoidmain(Stringargv[])throwsException{
4 Stringsentence;
5 StringmodifiedSentence;
6
7 BufferedReaderinFromUser=
8 newBufferedReader(
9 newInputStreamReader(System.in));
10
11 SocketclientSocket=newSocket("localhost",6789);
12
13 while(true){
14 DataOutputStreamoutToServer=
15 newDataOutputStream(
16 clientSocket.getOutputStream());
17
18 BufferedReaderinFromServer=
19 newBufferedReader(
20 newInputStreamReader(
21 clientSocket.getInputStream()));
22
23 sentence=inFromUser.readLine();
24
25 outToServer.writeBytes(sentence+'\n');
26
27 if(sentence.equals("EXIT")){
28 break;
29 }
30
31 modifiedSentence=inFromServer.readLine();
32
33 System.out.println("FROMSERVER:"+modifiedSentence);
34
35 }
36 clientSocket.close();
37 }
38 }

https://javadeveloperszone.wordpress.com/2013/04/20/javatcpchatmultipleclient/ 4/5
3/10/2017 JavaTCPChatMultipleClient|JavaDevelopersZone

Advertisements

APRIL20,2013 DAXJOSHI JAVACHATTING,


JAVATCPCHAT,TCPMULTIPLECLIENTSERVER
CHATTING

OnethoughtonJavaTCPChat
MultipleClient

1. Itaysays:
veryverynice!!!!!
thankyouverymuch!!!!

REPLY JULY23,2014AT2:34AM

CREATEAFREEWEBSITEORBLOGATWORDPRESS.COM.

https://javadeveloperszone.wordpress.com/2013/04/20/javatcpchatmultipleclient/ 5/5

You might also like