简体   繁体   中英

chat room using socket programming

i am writing program for chat room on client side i had wirtten

clientSocket = new Socket('127.0.0.1',5432);
socketInputBuffer = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
socketOutputBuffer = new BufferedWriter(new OutputStreamWriter(clientSocket.getOutputStream()));
systemBuffer = new BufferedReader(new InputStreamReader(System.in));

and on server side

sock = new ServerSocket(5432) ;

and socketinputbuffer and socketoutputbuffer as written in client

now in above code connection is properly established after that from server side i write in socket stream which i will be reading on the client.

but client waits for server to complete its writing. but when i use shutdownoutput on server it writes and client is able to read properly but same problem occurs when client writes. so i used shutdown output at client also. beacuse of this i am unable to send data next time pls suggest me any method so that client and server should work properly

i am writing using [CODE] socketOutputBuffer.write("Enter Your Name"); socketOutputBuffer.flush(); [/CODE] still not working.

Here's a quick code sample:

http://www.kieser.net/linux/java_server.html

To send data to the client without closing the connection, use the flush method on BufferedWriter. Also, you need to show how you're doing the reads/writes. You're probably using the blocking methods, in which case the client will wait till it receives data from the server to progress in the thread. You could split the code into two threads - a thread that writes to the server and a thread that reads from the server, or you can use Java NIO. You can use this project as a baseline.

尝试使用flush()方法发送数据而不关闭连接。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM