java socket编程,在eclipse平台编写客户端和服务端程序,两者要在不同的project里吗?服务端程序错在哪?
packagesocket;importjava.io.*;importjava.net.*;publicclassMyServer{publicstaticvoidma...
package socket;
import java.io.*;
import java.net.*;
public class MyServer {
public static void main(String[] args) throws IOException{
ServerSocket server=new ServerSocket(5678); /////////这一句报错了,错误提示在最下面
Socket client=server.accept();
BufferedReader in=new BufferedReader(new InputStreamReader(client.getInputStream()));
PrintWriter out=new PrintWriter(client.getOutputStream());
while(true){
String str=in.readLine();
System.out.println(str);
out.println("has receive.");
out.flush();
if(str.equals("end"))
break;
}
client.close();
server.close();
in.close();
}
}
Exception in thread "main" java.net.BindException: Address already in use: JVM_Bind
at java.net.DualStackPlainSocketImpl.bind0(Native Method)
at java.net.DualStackPlainSocketImpl.socketBind(Unknown Source)
at java.net.AbstractPlainSocketImpl.bind(Unknown Source)
at java.net.PlainSocketImpl.bind(Unknown Source)
at java.net.ServerSocket.bind(Unknown Source)
at java.net.ServerSocket.<init>(Unknown Source)
at java.net.ServerSocket.<init>(Unknown Source)
at socket.MyServer.main(MyServer.java:9) 展开
import java.io.*;
import java.net.*;
public class MyServer {
public static void main(String[] args) throws IOException{
ServerSocket server=new ServerSocket(5678); /////////这一句报错了,错误提示在最下面
Socket client=server.accept();
BufferedReader in=new BufferedReader(new InputStreamReader(client.getInputStream()));
PrintWriter out=new PrintWriter(client.getOutputStream());
while(true){
String str=in.readLine();
System.out.println(str);
out.println("has receive.");
out.flush();
if(str.equals("end"))
break;
}
client.close();
server.close();
in.close();
}
}
Exception in thread "main" java.net.BindException: Address already in use: JVM_Bind
at java.net.DualStackPlainSocketImpl.bind0(Native Method)
at java.net.DualStackPlainSocketImpl.socketBind(Unknown Source)
at java.net.AbstractPlainSocketImpl.bind(Unknown Source)
at java.net.PlainSocketImpl.bind(Unknown Source)
at java.net.ServerSocket.bind(Unknown Source)
at java.net.ServerSocket.<init>(Unknown Source)
at java.net.ServerSocket.<init>(Unknown Source)
at socket.MyServer.main(MyServer.java:9) 展开
1个回答
展开全部
端口冲突了,你是不是已经运行了一个服务端,或者这个端口已经被其他程序使用了。
追问
1、客户端程序(client.java)和服务端程序(myserver.java),这两个类是在同一个project里吗?
2、我只写了一个服务端程序
3、我如何知道这个端口是否被其他程序使用了?
追答
1、这个跟客户端没有关系,客户端和服务端在不在同一个project都无所谓;
2、是只有一个服务端程序,但可能重复运行;
3、如果你是在windows下面运行的话,在命令行中输入:netstat -an|findstr 5678
更多的命令在网上查一下
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询