简体   繁体   English

客户端套接字未接收到字符串

[英]Client socket is not receiving a string

I have created a simple client server socket program and sending a string a|b|c|* and the client is not recieving it. 我创建了一个简单的客户端服务器套接字程序,并发送了字符串a | b | c | *,但客户端未接收到它。 The client is on another machine. 客户端在另一台计算机上。

Server Code. 服务器代码。

String format = "a|b|c|*";
ServerSocket ss = new ServerSocket(2222);
System.out.println("Server Started.");

while (true) {              
    Socket s = ss.accept();
    System.out.println("Connection accepted.");
    InputStream is = s.getInputStream();
    InputStreamReader isr = new InputStreamReader(is);
    BufferedReader br = new BufferedReader(isr);
    PrintStream ps = new PrintStream(s.getOutputStream());
    ps.println(format);             
    ps.flush();
    System.out.println("Format sent.");
}    

Client Side 客户端

try {
    Socket s = new Socket("192.168.0.71", 2222);
    // step 3: Get I/O streams
    InputStream is = s.getInputStream();
    InputStreamReader isr= new InputStreamReader(is);
    BufferedReader br = new BufferedReader(isr);

    String format = br.readLine();
    System.out.println(format);
} catch(Exception ex){
    System.out.println(ex.getMessage());
}

When I create a client on the same machine then it is receiving the string, and when i pass this string to another machine client it doesn't receive it. 当我在同一台计算机上创建客户端时,它将接收该字符串,而当我将该字符串传递给另一台计算机客户端时,它将不会接收到该字符串。

So I tried your code on two of my boxes and remote connections worked fine. 因此,我在两个盒子上尝试了您的代码,并且远程连接运行良好。 A bunch of good comments but I thought I'd write out an answer with some things to try: 一堆好的评论,但我想我会写出一些尝试的答案:

  1. It's been implied that you've been able to telnet locally to the server and see the format string. 暗示您已经能够在本地telnet到服务器并查看格式字符串。 Fine. 精细。 Are you also been able to use telnet from a remote client to the server and also see the format string? 你还能够使用telnet从远程客户机到服务器, 看到了格式字符串?

  2. If the remote telnet does not work then I'd check if there is a firewall or otherwise a routing issue. 如果远程telnet不起作用,那么我将检查是否存在防火墙或其他路由问题。 Can you ping the server from the client? 您可以从客户端ping服务器吗?

  3. Another reason why telnet might not work is that your server may be binding to the wrong port. telnet可能无法正常工作的另一个原因是您的服务器可能绑定到错误的端口。 The default is to bind to all interfaces but just to be sure, you should check. 默认是绑定到所有接口,但是请确保检查一下。 If you are on a unix box or OSX then you can use the netstat command to see what ports you are binding to. 如果您在unix盒或OSX上,则可以使用netstat命令查看要绑定到的端口。 The following output shows that the server is bound to * which means all interfaces. 以下输出显示服务器绑定到* ,表示所有接口。

     $ netstat -an | grep 2222 tcp46 0 0 *.2222 *.* LISTEN 
  4. If nothing else helps then realize that if you have a telnet connected, it will block all other telnets and clients from connecting since you are not closing the socket on the server. 如果没有其他帮助,那么请意识到,如果您已连接telnet,则由于您没有关闭服务器上的套接字,因此它将阻止所有其他telnet和客户端连接。

Hope some of this helps. 希望有些帮助。

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

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