java Socket聊天系统错误,求解啊,谢谢各位啦!!!!!
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java Socket聊天系统错误,求解啊,谢谢各位啦!!!!!相关的知识,希望对你有一定的参考价值。
java.net.SocketException: select failed
at java.net.PlainSocketImpl.socketAccept(Native Method)
at java.net.PlainSocketImpl.accept(Unknown Source)
at java.net.ServerSocket.implAccept(Unknown Source)
at java.net.ServerSocket.accept(Unknown Source)
at TankServer.start(TankServer.java:18)
at TankServer.main(TankServer.java:33)
java 18: s = ss.accept();
java 33: new TankServer().start();
端口用的是 "172.0.0.1", 8888
程序是没有问题的,估计是端口问题,求解决方法!!!!
谢谢啦
端口是一致的,代码没问题,其他Socket程序运行是也是这个问题
追答我这里有个简单的例子发你看看!import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
public class SocketClient
public static void main(String[] args) throws IOException
Socket server = new Socket("127.0.0.1", 5678);
BufferedReader in = new BufferedReader(new InputStreamReader(server.getInputStream()));
PrintWriter out = new PrintWriter(server.getOutputStream());
BufferedReader wt = new BufferedReader(new InputStreamReader(System.in));
while (true)
String str = wt.readLine();
out.println(str);
out.flush();
if (str.equals("end"))
break;
System.out.println(in.readLine());
server.close();
public class SocketServer1 extends Thread
private Socket client;
public SocketServer1(Socket a)
client=a;
public void run()
try
BufferedReader in = new BufferedReader(new InputStreamReader(client.getInputStream()));
PrintWriter out = new PrintWriter(client.getOutputStream());
while(true)
String str=in.readLine();
int a=Integer.parseInt(str);
int b=a*a;
System.out.println(b);
out.println(b);
out.flush();
if(str.equals("end"))
break;
client.close();
catch(IOException ex)
finally
public static void main(String[] args)throws IOException
ServerSocket server=new ServerSocket(5678);
while(true)
SocketServer1 serverAll=new SocketServer1(server.accept());
serverAll.start();
不好意思,傍晚有事出去啦,你发给我的这个例子也是这种错误:Exception in thread "main" java.net.SocketException: select failed
at SocketServer1.main(SocketServer1.java:36)
java:36:SocketServer1 serverAll=new SocketServer1(server.accept());
1、首先在我电脑上时能跑的,有可能端口被占用,你试试换个端口不过要保持客户端和服务端一致。
2、可能是你电脑的问题,你可以换个电脑跑下那段程序
3、2段程序先要打开服务端再打开客户端你看看是不是这个原因
4、实在不行只能重装下看看了。
谢谢你的回答啦,就是我电脑的问题,ServerSocket无法自动获取ip和端口,现在解决啦,直接在构造函数指定ip和端口,谢谢啦,(我的网络协议估计出问题啦,获取不了ip)
参考技术A 172.0.0.1 ???? 试试 127.0.0.1追问一样,不行
以上是关于java Socket聊天系统错误,求解啊,谢谢各位啦!!!!!的主要内容,如果未能解决你的问题,请参考以下文章