Java多线程客户机-服务器套接字编程
我正在开发的一款短信应用程序出现问题,希望有人能给我指出正确的方向。我一直无法让客户端和服务器正确通信 我的客户机向5个进程多播一条消息,这些进程在服务器端进行更改并将其发送回客户机。这一切都很好,但是当我第二次尝试多播时,服务器在读取时陷入困境,并最终在我在代码中指出的行处给出错误:W/System.err(没有其他信息) 客户端代码:Java多线程客户机-服务器套接字编程,java,multithreading,sockets,server,client,Java,Multithreading,Sockets,Server,Client,我正在开发的一款短信应用程序出现问题,希望有人能给我指出正确的方向。我一直无法让客户端和服务器正确通信 我的客户机向5个进程多播一条消息,这些进程在服务器端进行更改并将其发送回客户机。这一切都很好,但是当我第二次尝试多播时,服务器在读取时陷入困境,并最终在我在代码中指出的行处给出错误:W/System.err(没有其他信息) 客户端代码: private class ClientTask extends AsyncTask<String, Void, Void> { @Ov
private class ClientTask extends AsyncTask<String, Void, Void> {
@Override
protected Void doInBackground(String... msgs) {
try{
String msg = msgs[0];
int ID = Integer.parseInt(msgs[1]);
ObjectOutputStream out;
clock++;
Message m1 = new Message(clock, msg, ID);
for (int i = 0; i < 5; ++i) {
String remotePort = REMOTE_PORT[i];
Socket socket = new Socket(InetAddress.getByAddress(new byte[]{10, 0, 2, 2}),
Integer.parseInt(remotePort));
out = new ObjectOutputStream(socket.getOutputStream());
out.writeObject(m1);
out.flush();
ObjectInputStream in = new ObjectInputStream(socket.getInputStream());
Message m2 = (Message)in.readObject();
proposed_seq_from_q = m2.seq_number;
all_proposed.add(proposed_seq_from_q);
socket.close();
}
agreed_deliver = Collections.max(all_proposed);
m1.set_seq_number(agreed_deliver);
for (int i = 0; i < 5; ++i) {
String remotePort = REMOTE_PORT[i];
Socket socket = new Socket(InetAddress.getByAddress(new byte[]{10, 0, 2, 2}),
Integer.parseInt(remotePort));
out = new ObjectOutputStream(socket.getOutputStream());
out.writeObject(m1);
out.flush();
socket.close();
}
} catch(ClassNotFoundException e){
Log.e(TAG, "ClientTask: ClassNotFoundException");
e.printStackTrace();
} catch (UnknownHostException e) {
Log.e(TAG, "ClientTask: UnknownHostException");
e.printStackTrace();
} catch (IOException e) {
Log.e(TAG, "ClientTask: IOException");
e.printStackTrace();
}
return null;
}
}
私有类ClientTask扩展了AsyncTask{
@凌驾
受保护的空doInBackground(字符串…msgs){
试一试{
字符串msg=msgs[0];
intid=Integer.parseInt(msgs[1]);
对象输出流输出;
时钟++;
消息m1=新消息(时钟、消息、ID);
对于(int i=0;i<5;++i){
字符串remotePort=REMOTE_PORT[i];
套接字套接字=新套接字(InetAddress.getByAddress(新字节[]{10,0,2,2}),
parseInt(remotePort));
out=newObjectOutputStream(socket.getOutputStream());
out.writeObject(m1);
out.flush();
ObjectInputStream in=新的ObjectInputStream(socket.getInputStream());
Message m2=(Message)在.readObject()中;
从q=m2的建议序号;
所有提议。添加(提议从提议开始);
socket.close();
}
同意交付=Collections.max(所有提议);
m1.设置序号(约定交付);
对于(int i=0;i<5;++i){
字符串remotePort=REMOTE_PORT[i];
套接字套接字=新套接字(InetAddress.getByAddress(新字节[]{10,0,2,2}),
parseInt(remotePort));
out=newObjectOutputStream(socket.getOutputStream());
out.writeObject(m1);
out.flush();
socket.close();
}
}catch(classnotfounde异常){
Log.e(标记“ClientTask:ClassNotFoundException”);
e、 printStackTrace();
}捕获(未知后异常e){
Log.e(标记“ClientTask:UnknownHostException”);
e、 printStackTrace();
}捕获(IOE异常){
Log.e(标记“ClientTask:IOException”);
e、 printStackTrace();
}
返回null;
}
}
服务器代码:
private class ServerTask extends AsyncTask<ServerSocket, String, Void> {
@Override
protected Void doInBackground(ServerSocket... sockets) {
ServerSocket serverSocket = sockets[0];
boolean listening = true;
try{
while(listening) {
Socket clientSocket = serverSocket.accept();
ObjectInputStream in = new ObjectInputStream(clientSocket.getInputStream());
Message m = (Message)in.readObject();
new_proposed = Math.max(new_proposed + 1, clock);
m.set_seq_number(new_proposed);
ObjectOutputStream out = new ObjectOutputStream(clientSocket.getOutputStream());
out.writeObject(m);
holdback_queue.add(m);
// ******************************************************
// THIS IS WHERE IT IS CRASHING
// ******************************************************
m = (Message)in.readObject();
publishProgress(m.get_message());
}
}catch(Exception e){
Log.e(TAG, "FAIL");
e.printStackTrace();
}
return null;
}
私有类ServerTask扩展了AsyncTask{
@凌驾
受保护的空doInBackground(服务器插座…插座){
ServerSocket ServerSocket=套接字[0];
布尔监听=真;
试一试{
边听{
Socket clientSocket=serverSocket.accept();
ObjectInputStream in=新的ObjectInputStream(clientSocket.getInputStream());
Message m=(Message)在.readObject()中;
新建议=数学最大值(新建议+1,时钟);
m、 设置序号(新提议);
ObjectOutputStream out=新的ObjectOutputStream(clientSocket.getOutputStream());
out.writeObject(m);
延迟队列。添加(m);
// ******************************************************
//这就是它崩溃的地方
// ******************************************************
m=(消息)在.readObject()中;
publishProgress(m.get_message());
}
}捕获(例外e){
Log.e(标记“失败”);
e、 printStackTrace();
}
返回null;
}
我已经尝试了所有我能想到的方法,但是我无法让服务器读取第二条消息。在那之前,一切都在正常工作,并且没有任何问题。有人能告诉我我做错了什么吗