Sunteți pe pagina 1din 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

S-ar putea să vă placă și