Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/windows/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java,客户机-服务器通信_Java_Windows_Eclipse_Socket.io - Fatal编程技术网

Java,客户机-服务器通信

Java,客户机-服务器通信,java,windows,eclipse,socket.io,Java,Windows,Eclipse,Socket.io,我有一个小server.jar,它在端口10000上侦听GET和END命令 我的客户代码是: package communication; import java.io.*; import java.net.*; public class Client { public static void main(String args[]) throws Exception { try { Socket socket = null; Pri

我有一个小server.jar,它在端口10000上侦听GET和END命令

我的客户代码是:

package communication;

import java.io.*;
import java.net.*;

public class Client {

public static void main(String args[]) throws Exception {
    try {

            Socket socket = null;
            PrintWriter out = null;
            BufferedReader in = null;

            socket = new Socket("localhost",10000);
            System.out.println("SOCKET = " + socket);
            System.out.print(socket.getInetAddress() + "\n");
            System.out.print(socket.getInputStream() + "\n");
            System.out.println(socket.isConnected() + "\n");

            out = new PrintWriter(socket.getOutputStream(),true);

            in = new BufferedReader(new 
                            InputStreamReader(socket.getInputStream()));

            String str = "GET";
            out.println(str);   
            String reponse = in.readLine();
            System.out.println(socket.isConnected() + "\n");
            for(int i = 0; i < 10; i++){
                    out.println(str);          // envoi d'un message
                    reponse = in.readLine();      // lecture de la reponse
                    System.out.println("Forme recue: " + reponse);
            }
            System.out.println("END");     // message de terminaison
            out.println("END") ;
            in.close();
            out.close();
            socket.close();

    }
    catch(IOException e) {
            System.out.println(e.getCause());
    }
}
}
包通信;
导入java.io.*;
导入java.net。*;
公共类客户端{
公共静态void main(字符串args[])引发异常{
试一试{
套接字=空;
PrintWriter out=null;
BufferedReader in=null;
套接字=新套接字(“本地主机”,10000);
System.out.println(“SOCKET=“+SOCKET”);
System.out.print(socket.getInetAddress()+“\n”);
System.out.print(socket.getInputStream()+“\n”);
System.out.println(socket.isConnected()+“\n”);
out=新的PrintWriter(socket.getOutputStream(),true);
in=新的BufferedReader(新的
InputStreamReader(socket.getInputStream());
String str=“GET”;
out.println(str);
字符串响应=in.readLine();
System.out.println(socket.isConnected()+“\n”);
对于(int i=0;i<10;i++){
out.println(str);//envoi d'un消息
reponse=in.readLine();//reponse讲座
System.out.println(“Forme recue:+Response”);
}
System.out.println(“END”);//消息终止
out.println(“结束”);
in.close();
out.close();
socket.close();
}
捕获(IOE异常){
System.out.println(e.getCause());
}
}
}
我知道这段代码有效,因为它在我的一台计算机上运行。但是,我不能让它在另一台上运行。两者的配置都是:Windows764、JRE6和Eclipse

我的server.jar应用程序打开了一个小GUi,让我知道通信是否打开,这在计算机上从来都不是这样的,顺便说一句,在readLine()行上卡住了

我试图关闭Windows防火墙、防病毒。。。什么都没用

有人知道这里出了什么问题吗


谢谢

首先要尝试的是-在out.println之后添加一个out.flush()。

我盯着
localhost
字符串,预感这可能是问题所在

在这个问题的答案中,可能需要对
::localhost
进行注释,并且在C:\Windows\System32\drivers\etc\hosts文件中将localhost硬编码为127.0.0.1


通过使用特定IP地址对
localhost
进行硬编码,服务器将侦听与客户端程序尝试连接的
localhost
相同的
localhost。

我们需要更多信息来帮助您。你会犯什么错误?这是什么行为等。我没有得到任何错误,程序只是停留在字符串response=in.readLine()行;就我而言,困扰我的其实是缺乏行为;)你到底想测试什么?如果需要服务器-客户机通信,为什么没有任何SocketServer?服务器只是一个小小的可执行jar。我启动它,然后它监听端口10000,等待GET和END命令。我用Telnet进行了测试,成功了,我只是尝试了一下,什么都没有,仍然停留在readLine()。我强烈怀疑我必须更改代码,因为它在另一台计算机上工作。。。那一定是另外一回事。。。我想:)有一件事我没有提到,System.out.println(socket.isConnected()+“\n”);返回true。您是对的,您正在启用自动刷新。您确定服务器正在接收您的请求并打印响应吗?为了好玩,您可以尝试在每个println之后关闭输出流(然后在每个println之前打开一个新的输出流);但是,在服务器端,即在我的server.jar GUI上,没有出现连接。使用Putty telnet,在同一个端口(10000)上,服务器的GUI上会弹出life,我可以手动发送GET和END命令,并从服务器获取我想要的,作为回报…您的服务器端是否使用ServerSocket?有趣的是,我今晚会尝试,我会随时通知您。谢谢