简体   繁体   English

使用套接字编程的聊天室

[英]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) ; sock = new ServerSocket(5432) ;

and socketinputbuffer and socketoutputbuffer as written in client socketinputbuffersocketoutputbuffer写在客户端

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. 但是,当我在服务器上使用shutdownoutput时,它会写入并且客户端能够正确读取,但是客户端写入时会发生相同的问题。 so i used shutdown output at client also. 所以我也在客户端使用了shutdown输出。 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"); 我正在使用[CODE] socketOutputBuffer.write(“输入您的名字”); socketOutputBuffer.flush(); socketOutputBuffer.flush(); [/CODE] still not working. [/ CODE]仍然无法正常工作。

Here's a quick code sample: 这是一个快速的代码示例:

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

To send data to the client without closing the connection, use the flush method on BufferedWriter. 要将数据发送到客户端而不关闭连接,请使用BufferedWriter上的flush方法。 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. 您可以将代码分成两个线程-一个写入服务器的线程和一个从服务器读取的线程,或者可以使用Java NIO。 You can use this project as a baseline. 您可以将此项目用作基准。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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