每个客户端请求的Java DatagramSocket不同线程
我正在从事一个Java项目,其中客户端通过DatagramSocket发送一个基本计算(例如5+6),服务器需要回复结果。目标是让不同的线程计算每个不同的客户端请求。到目前为止,我有以下代码: 客户:每个客户端请求的Java DatagramSocket不同线程,java,java-threads,Java,Java Threads,我正在从事一个Java项目,其中客户端通过DatagramSocket发送一个基本计算(例如5+6),服务器需要回复结果。目标是让不同的线程计算每个不同的客户端请求。到目前为止,我有以下代码: 客户: import java.net.*; import java.io.*; public class UDPClient { public static void main (String args[]){ DatagramSocket aSocket = null;
import java.net.*;
import java.io.*;
public class UDPClient {
public static void main (String args[]){
DatagramSocket aSocket = null;
int input=-1;
String operation=null;
while(input!=0){
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Select an option");
System.out.println("1. Do a basic calculation");
System.out.println("0. Exit ");
try{
input = Integer.parseInt(br.readLine());
if(input>1 | input<0)throw new Exception("Error");
}
catch(Exception e){
System.out.println("Wrong selection...Try again \n");
}
if(input==1)
{
System.out.println("Give operator symbol (+,-,*,/) ");
try{
String operator = br.readLine();
if(!(operator.matches("[*]|[+]|[/]|[-]")))throw new Exception("Error");
System.out.println("Give first number");
int first = Integer.parseInt(br.readLine());
System.out.println("Give second number");
int second = Integer.parseInt(br.readLine());
operation = first+":"+operator+":"+second;
send:
try{
aSocket = new DatagramSocket();
byte [] m = operation.getBytes();
InetAddress aHost = InetAddress.getByName(args[1]);
int serverPort = 6789;
DatagramPacket request = new DatagramPacket(m, m.length, aHost, serverPort);
aSocket.send(request);
aSocket.setSoTimeout(10000); // SocketTimeout happens here
byte[]buffer = new byte[1000];
DatagramPacket reply = new DatagramPacket(buffer, buffer.length);
aSocket.receive(reply);
System.out.println("Reply:" + new String(reply.getData()));
}
catch (SocketTimeoutException e) {
System.out.println("Server reply timeout");
break send; //If Timeout happens, send request again
}
catch(SocketException e){
System.out.println("Socket: " + e.getMessage());
}
catch(IOException e){
System.out.println("IO: " + e.getMessage());
}
finally {if (aSocket!=null) aSocket.close();}
}
catch(Exception e){
System.out.println("Wrong input...Try again \n");
}
}//End of if
}//end of while
}
}
import java.net.*;
import java.io.*;
public class UDPClient {
public static void main (String args[]){
DatagramSocket aSocket = null;
int input=-1; //User Input from menu
String operation=null; //keeps the calculation operation in String form
int resends=0; //NACK counter
while(input!=0){
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Select an option");
System.out.println("1. Do a basic calculation");
System.out.println("0. Exit ");
try{
input = Integer.parseInt(br.readLine());
if(input>1 | input<0)throw new Exception("Error");
}
catch(Exception e){
System.out.println("Wrong selection...Try again \n");
}
if(input==1)
{
System.out.println("Give operator symbol (+,-,*,/) ");
try{
String operator = br.readLine();
if(!(operator.matches("[*]|[+]|[/]|[-]")))throw new Exception("Error");
System.out.println("Give first number");
int first = Integer.parseInt(br.readLine());
System.out.println("Give second number");
int second = Integer.parseInt(br.readLine());
operation = first+":"+operator+":"+second;
}
catch(Exception e){
System.out.println("Wrong input...Try again \n");
System.out.println(e.getMessage());
}
while(true){
try{
if(resends==3){ // At 3 resends break loop
resends=0;
break;
}
aSocket = new DatagramSocket();
byte [] m = operation.getBytes();
InetAddress aHost = InetAddress.getByName(args[1]);
int serverPort = 6789;
DatagramPacket request = new DatagramPacket(m, m.length, aHost, serverPort);
aSocket.send(request);
resends++;//counting times we send this message
aSocket.setSoTimeout(30000); // Setting timeout for socket (30 sec)
byte[]buffer = new byte[1000];
DatagramPacket reply = new DatagramPacket(buffer, buffer.length);
aSocket.receive(reply);
System.out.println("Reply:" + new String(reply.getData()));
if(reply!=null)break; // If you get a reply, break loop
}
catch (SocketTimeoutException e) {
System.out.println("Server reply timeout");
}
catch(SocketException e){
System.out.println("Socket: " + e.getMessage());
}
catch(IOException e){
System.out.println("IO: " + e.getMessage());
}
finally {if (aSocket!=null) aSocket.close();}
}// end of send while loop
}//End of if
else if (input==0)
System.out.println("Exiting UDP Client.....");
}//end of while
}//end of main
}//end of Class
我有两个基本问题
1) 如果我从客户端发送第二个请求(第一个请求执行得很好),则会导致服务器输出套接字关闭错误消息
2) 正如您在服务器端看到的,我目前已经对sock.send(reply)进行了注释,以便检查我在客户端添加的超时代码。问题是,在超时发生后(因为从来没有发送回复),代码从开头开始,而不是我在TimeoutException catch()中指定的发送标签
如果有人能帮我解决这两个问题中的任何一个,我将不胜感激
提前感谢您的时间 在循环之前创建套接字。目前你让它被垃圾收集
您关于套接字超时发生在
setSoTimeout()
调用上的声明不正确。它发生在receive()。让我知道你对此的看法
客户:
import java.net.*;
import java.io.*;
public class UDPClient {
public static void main (String args[]){
DatagramSocket aSocket = null;
int input=-1;
String operation=null;
while(input!=0){
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Select an option");
System.out.println("1. Do a basic calculation");
System.out.println("0. Exit ");
try{
input = Integer.parseInt(br.readLine());
if(input>1 | input<0)throw new Exception("Error");
}
catch(Exception e){
System.out.println("Wrong selection...Try again \n");
}
if(input==1)
{
System.out.println("Give operator symbol (+,-,*,/) ");
try{
String operator = br.readLine();
if(!(operator.matches("[*]|[+]|[/]|[-]")))throw new Exception("Error");
System.out.println("Give first number");
int first = Integer.parseInt(br.readLine());
System.out.println("Give second number");
int second = Integer.parseInt(br.readLine());
operation = first+":"+operator+":"+second;
send:
try{
aSocket = new DatagramSocket();
byte [] m = operation.getBytes();
InetAddress aHost = InetAddress.getByName(args[1]);
int serverPort = 6789;
DatagramPacket request = new DatagramPacket(m, m.length, aHost, serverPort);
aSocket.send(request);
aSocket.setSoTimeout(10000); // SocketTimeout happens here
byte[]buffer = new byte[1000];
DatagramPacket reply = new DatagramPacket(buffer, buffer.length);
aSocket.receive(reply);
System.out.println("Reply:" + new String(reply.getData()));
}
catch (SocketTimeoutException e) {
System.out.println("Server reply timeout");
break send; //If Timeout happens, send request again
}
catch(SocketException e){
System.out.println("Socket: " + e.getMessage());
}
catch(IOException e){
System.out.println("IO: " + e.getMessage());
}
finally {if (aSocket!=null) aSocket.close();}
}
catch(Exception e){
System.out.println("Wrong input...Try again \n");
}
}//End of if
}//end of while
}
}
import java.net.*;
import java.io.*;
public class UDPClient {
public static void main (String args[]){
DatagramSocket aSocket = null;
int input=-1; //User Input from menu
String operation=null; //keeps the calculation operation in String form
int resends=0; //NACK counter
while(input!=0){
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Select an option");
System.out.println("1. Do a basic calculation");
System.out.println("0. Exit ");
try{
input = Integer.parseInt(br.readLine());
if(input>1 | input<0)throw new Exception("Error");
}
catch(Exception e){
System.out.println("Wrong selection...Try again \n");
}
if(input==1)
{
System.out.println("Give operator symbol (+,-,*,/) ");
try{
String operator = br.readLine();
if(!(operator.matches("[*]|[+]|[/]|[-]")))throw new Exception("Error");
System.out.println("Give first number");
int first = Integer.parseInt(br.readLine());
System.out.println("Give second number");
int second = Integer.parseInt(br.readLine());
operation = first+":"+operator+":"+second;
}
catch(Exception e){
System.out.println("Wrong input...Try again \n");
System.out.println(e.getMessage());
}
while(true){
try{
if(resends==3){ // At 3 resends break loop
resends=0;
break;
}
aSocket = new DatagramSocket();
byte [] m = operation.getBytes();
InetAddress aHost = InetAddress.getByName(args[1]);
int serverPort = 6789;
DatagramPacket request = new DatagramPacket(m, m.length, aHost, serverPort);
aSocket.send(request);
resends++;//counting times we send this message
aSocket.setSoTimeout(30000); // Setting timeout for socket (30 sec)
byte[]buffer = new byte[1000];
DatagramPacket reply = new DatagramPacket(buffer, buffer.length);
aSocket.receive(reply);
System.out.println("Reply:" + new String(reply.getData()));
if(reply!=null)break; // If you get a reply, break loop
}
catch (SocketTimeoutException e) {
System.out.println("Server reply timeout");
}
catch(SocketException e){
System.out.println("Socket: " + e.getMessage());
}
catch(IOException e){
System.out.println("IO: " + e.getMessage());
}
finally {if (aSocket!=null) aSocket.close();}
}// end of send while loop
}//End of if
else if (input==0)
System.out.println("Exiting UDP Client.....");
}//end of while
}//end of main
}//end of Class
在客户端,args[1]表示服务器的IP地址。如果您的服务器在本地主机上运行,您可以使用“127.0.0.1”。很久没有看到有人在java代码中使用标签了!我也不推荐它。对于点code
操作符处的正则表达式,匹配(“[*].[+].[/].[-]”code
您可以使用code
操作符。匹配([*+/-]”code
代替。很抱歉,刚刚获得了“到处注释”特权,了解如何使用标签。谢谢大家抽出时间,我会尝试你们建议的更正。谢谢你们抽出时间,但我无法理解你们的更正。你能说得更具体些吗?