这是服务器类的代码。
try{
//1. creating a server socket
providerSocket = new ServerSocket(2004, 10);
//2. Wait for connection
System.out.println("Waiting for connection");
connection = providerSocket.accept();
//connections++;
System.out.println("Connection received from " + connection.getInetAddress().getHostName());
//3. get Input and Output streams
out = new ObjectOutputStream(connection.getOutputStream());
out.flush();
in = new ObjectInputStream(connection.getInputStream());
message = new Message();
//sendMessage(message);
//4. The two parts communicate via the input and output streams
do{
try{
message = (Message)in.readObject();
System.out.println("client>" + message.status);
System.out.println(message.status);
Protocol pro = new Protocol();
message.setProtocol(pro);
sendMessage(message);
message.connect = false;
}
catch(ClassNotFoundException classnot){
System.err.println("Data received in unknown format");
}
}while(message.connect);
}
catch(IOException ioException){
ioException.printStackTrace();
}
finally{
//4: Closing connection
try{
in.close();
out.close();
providerSocket.close();
}
catch(IOException ioException){
ioException.printStackTrace();
}
}
}
void sendMessage(Message msg)
{
try{
out.writeObject(msg);
out.flush();
System.out.println("server>" + msg.status);
}
catch(IOException ioException){
ioException.printStackTrace();
}
}
public void run()
{
try{
//1. creating a socket to connect to the server
requestSocket = new Socket(InetAddress.getLocalHost(), 2004);
message = new Message();
System.out.println("Connected to localhost in port 2004");
//2. get Input and Output streams
out = new ObjectOutputStream(requestSocket.getOutputStream());
out.flush();
in = new ObjectInputStream(requestSocket.getInputStream());
//3: Communicating with the server
do{
try{
message.status = "connected";
message.actionUser.setUserID(1);
message.actionUser.setPassword("bear");
sendMessage(message);
message = (Message)in.readObject();
Object output = message.getProtocol().processInput(message);
message.connect = false;
message.status = "Disconnected";
//sendMessage(message);
}
catch(Exception e){
System.err.println("data received in unknown format");
System.err.println(e.getMessage());
}
}while(message.connect);
}
catch(UnknownHostException unknownHost){
System.err.println("You are trying to connect to an unknown host!");
}
catch(IOException ioException){
ioException.printStackTrace();
}
finally{
//4: Closing connection
try{
if(requestSocket.isInputShutdown())
in.close();
if(requestSocket.isOutputShutdown())
out.close();
requestSocket.close();
}
catch(IOException ioException){
ioException.printStackTrace();
}
}
}
Connected to localhost in port 2004
Connected to localhost in port 2004
client>connected
java.net.SocketException: Connection reset
at java.net.SocketInputStream.read(Unknown Source)
at java.net.SocketInputStream.read(Unknown Source)
at java.io.ObjectInputStream$PeekInputStream.read(Unknown Source)
at java.io.ObjectInputStream$PeekInputStream.readFully(Unknown Source)
at java.io.ObjectInputStream$BlockDataInputStream.readShort(Unknown Source)
at java.io.ObjectInputStream.readStreamHeader(Unknown Source)
at java.io.ObjectInputStream.<init>(Unknown Source)
at auction.Requester.run(Requester.java:22)
at java.lang.Thread.run(Unknown Source)
如有任何帮助,不胜感激
使您的服务器多线程,这样它就可以在不同的线程中处理每个客户机。
试试这个:
Class ConnectionThread extends Thread {
Socket connection;
ObjectInputStream in;
OjectOutputStream out;
Message message;
Connection Thread(Socket connection) {
this.connection = connection;
}
void run() {
try {
System.out.println("Connection received from " + connection.getInetAddress().getHostName());
out = new ObjectOutputStream(connection.getOutputStream());
out.flush();
in = new ObjectInputStream(connection.getInputStream());
message = new Message();
do{
try{
message = (Message)in.readObject();
System.out.println("client>" + message.status);
System.out.println(message.status);
Protocol pro = new Protocol();
message.setProtocol(pro);
sendMessage(message);
message.connect = false;
} catch(ClassNotFoundException classnot){
System.err.println("Data received in unknown format");
}
} while(message.connect);
}
catch(IOException ioException){
ioException.printStackTrace();
}
finally{
try{
in.close();
out.close();
providerSocket.close();
} catch(IOException ioException){
ioException.printStackTrace();
}
}
}
void sendMessage(Message msg) {
try{
out.writeObject(msg);
out.flush();
System.out.println("server>" + msg.status);
} catch(IOException ioException){
ioException.printStackTrace();
}
}
线程“main”java.net.ConnectException:连接超时:在java.net.dualStackplainsockeTimpl.Connect0(本机方法)在java.net.dualStackplainsockeTimpl.socketConnect(DualStackplainsockeTimpl.java:69)在java.net.abstractplainsockeTi
服务器 ServerThreadHandler:
我编写了一个简单的java套接字服务器。我可以使用java客户端连接到它。 但是,当我尝试将android设备连接到它时,它会给出以下错误: 代码如下: 这一行:似乎是罪魁祸首。 错误消息“java.net.socketexception:套接字失败:EACCES(权限被拒绝)” Android java.net.SocketException:套接字失败:EACCES(拒绝权限) Android
为了实现这一点,我使用了队列/线程池机制。最初,我创建一个固定数量线程的池,并有一个队列datastructure来存储客户机地址。这个队列在所有线程之间共享,因此我使用“互斥”来锁定/解锁这个队列。在主服务器线程中,我创建一个套接字,将其绑定到全局端口/地址,然后在“recvfrom”调用上阻止服务器。任何希望与服务器通信的客户端都会向侦听全局端口地址的主线程服务器发送“HI”消息。主服务器线程
问题内容: 我必须使用Java的套接字API编写多线程客户端和服务器。客户端和服务器都是多线程的,因此服务器可以处理多个连接,客户端可以测试服务器处理连接的能力。 我的代码在这里:https : //github.com/sandyw/Simple-Java-Client- Server 我有几个可能是相关的问题。一,偶尔会有一个客户端线程抛出 从其位置来看,这意味着服务器在客户端完成从套接字读取
首先我要感谢你花时间... 我在macbook中用C++创建了一个服务器套接字,在运行windows XP的不同机器中用Java创建了一个客户机/套接字。我已将端口指定为5000,但无法指定正确的主机,因此无法进行连接。当我在windows xp中使用WinSock2创建一个C++服务器/套接字时,当我使用本地主机时,连接是完美的...有什么想法吗??? 提前Thnx int main(int a