在java netbeans中在客户端服务器中连续聊天 [英] continuous chat in client server in java netbeans
本文介绍了在java netbeans中在客户端服务器中连续聊天的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想让客户端 - 服务器聊天代码,我只做一次向服务器发送消息而不是从服务器到客户端
客户代码
i want to make client-server chat the code which i make it only once sending message to server but not from server to client
Client code
public class Chat_Client {
public static void client ()
{
try
{
Socket cs = new Socket("localhost", 9);
PrintStream cp = new PrintStream(cs.getOutputStream());
System.out.print("Client: ");
InputStreamReader cir = new InputStreamReader(System.in);
BufferedReader cbr = new BufferedReader(cir);
String temp1 = cbr.readLine();
cp.println(temp1);
BufferedReader cbr1 = new BufferedReader(new InputStreamReader(cs.getInputStream()));
String temp2 = cbr1.readLine();
System.out.print(temp2);
}
catch(Exception e)
{
}
}
public static void main(String[] args) {
client();
}
-------------- -------------------------------------------------- ------------------------------
服务器代码
----------------------------------------------------------------------------------------------
server code
public class Chat_server1 {
public static void server()
{
try
{
ServerSocket ss = new ServerSocket(9);
Socket cs = ss.accept();
BufferedReader cbr = new BufferedReader(new InputStreamReader(cs.getInputStream()));
String temp = cbr.readLine();
System.out.print("Client :" +temp);
//JOptionPane.showMessageDialog(null,"Salam");
PrintStream spr = new PrintStream(cs.getOutputStream());
String temp1 = "Server: I got your message ";
spr.println(temp1);
//server();
}
catch(Exception e)
{
}
}
public static void main(String[] args) {
// TODO code application logic here
server();
}
推荐答案
这篇关于在java netbeans中在客户端服务器中连续聊天的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文