用JAVA建立客户机-服务器通信?
我想在客户端和服务器之间执行双向通信,到目前为止,我已经实现了单向通信。 我的JAVA代码看起来像 服务器端用JAVA建立客户机-服务器通信?,java,sockets,Java,Sockets,我想在客户端和服务器之间执行双向通信,到目前为止,我已经实现了单向通信。 我的JAVA代码看起来像 服务器端 public class server{ @SuppressWarnings("deprecation") public static void main(String[] args) { try{ ServerSocket s=new ServerSocket(9998);
public class server{
@SuppressWarnings("deprecation")
public static void main(String[] args)
{
try{
ServerSocket s=new ServerSocket(9998);
Socket ss=s.accept();
DataInputStream din=new DataInputStream(ss.getInputStream());
DataInputStream uip=new DataInputStream(System.in);
DataOutputStream dout=new DataOutputStream(ss.getOutputStream());
System.out.println("Enter message to send to client\n");
String stc=uip.readLine();
dout.writeBytes(""+stc);
din.close();
dout.close();
uip.close();
}
catch(Exception e)
{
System.out.println(e);
}`enter code here`
}
}
public class client{
public static void main(String[] args)
{
try{
Socket ss=new Socket("localhost",9998);
DataInputStream din=new DataInputStream(ss.getInputStream());
DataInputStream uip=new DataInputStream(System.in);
DataOutputStream dout=new DataOutputStream(ss.getOutputStream());
String msg=din.readLine();
System.out.println("Received msg is "+msg);
din.close();
dout.close();
uip.close();
}
catch(Exception e)
{
System.out.println(e);
}
}
}
客户端
public class server{
@SuppressWarnings("deprecation")
public static void main(String[] args)
{
try{
ServerSocket s=new ServerSocket(9998);
Socket ss=s.accept();
DataInputStream din=new DataInputStream(ss.getInputStream());
DataInputStream uip=new DataInputStream(System.in);
DataOutputStream dout=new DataOutputStream(ss.getOutputStream());
System.out.println("Enter message to send to client\n");
String stc=uip.readLine();
dout.writeBytes(""+stc);
din.close();
dout.close();
uip.close();
}
catch(Exception e)
{
System.out.println(e);
}`enter code here`
}
}
public class client{
public static void main(String[] args)
{
try{
Socket ss=new Socket("localhost",9998);
DataInputStream din=new DataInputStream(ss.getInputStream());
DataInputStream uip=new DataInputStream(System.in);
DataOutputStream dout=new DataOutputStream(ss.getOutputStream());
String msg=din.readLine();
System.out.println("Received msg is "+msg);
din.close();
dout.close();
uip.close();
}
catch(Exception e)
{
System.out.println(e);
}
}
}
我尝试在客户端获取输入,并尝试以相同的方式发送到服务器。但这不起作用。我哪里做错了?我应该如何实现双向沟通
在客户端,我从用户那里获得输入并使用
.writeBytes(value)代码>并在服务器端的din中执行readLine()
,正如我在上面的单向通信中所做的那样。但这不起作用。我哪里做错了?我假设您在不同的进程中运行这两个程序,并且您只希望在从服务器发送的客户端上得到结果。从客户端,您不会向服务器发送任何内容
如果没有收到任何错误,请尝试刷新流,如:
dout.writeBytes(""+stc);
dout.flush();
来自注释:添加了以下代码
在服务器端接收代码
// this is your code on server
dout.writeBytes(""+stc);
//Add this code
String msgServer = din.readLine();
System.out.println("Received msg on Server: " + msgServer );
添加客户端代码:
// this is your code on client
System.out.println("Received msg is "+msg);
dout.writeBytes("Test data from client");
dout.flush();
创建双通信系统的最佳方法是创建两个线程,一个用于编写消息,另一个用于接收消息(客户端和服务器端)
侦听线程接收到一条消息,唤醒自己并执行某些操作。如果需要响应,它将创建响应并将其添加到队列中
写入线程定期检查要发送的消息队列。如果有,它会发送它们。这是双向客户机-服务器通信的最佳方式。。“dout.shutdownOutput();”在客户端的send函数之后编写这行代码。这是名为half socket closed的函数,它将有助于来回通信。您遇到了什么错误?我认为您正在从服务器向客户端发送一些内容,但从未从客户端向服务器发送(从代码看).通信应为发送-接收(双向)。(1) 客户端没有向服务器发送任何内容(2)客户端没有读取服务器在套接字上发送的内容。我只提到了代码的工作部分。当我尝试执行相同的操作(另一种通信方式)时,我没有收到任何错误,但无法获得任何输出。因为您既没有向服务器发送任何内容,也没有在服务器端接收任何内容。