如何创建同时侦听UDP和UDP的服务器Java中的TCP客户端

问题描述:

我有一个问题,我创建了一个TCP客户端和一个UDP客户端,需要创建一个可以与我的两个代码进行通信的服务器:


TCP客户端:

I have a problem I created a TCP Client and a UDP Client and need to create a Server that can communicate with both my code is listed down:


TCP Client:

import java.io.*; 
import java.net.*; 
class TCPClient { 
    public static void main(String args[]) throws Exception 
    { 
		try
		{
			String Message;
			Socket cs= new Socket("localhost", 10000);
			String M;	
			BufferedReader toServer= new BufferedReader(new InputStreamReader(System.in));
			Message = toServer.readLine();
			
			DataOutputStream toS= new 	DataOutputStream(cs.getOutputStream());  
			toS.writeBytes(Message+'\n');
			
			BufferedReader fromServer= new 	BufferedReader(new InputStreamReader(cs.getInputStream()));
			M = fromServer.readLine();
			System.out.println("from server:"+M+" SUCCESS!!!");
			cs.close();
		}
		catch (Exception ex)
		{
		
		}
	}   	
} 






UDP客户端:






UDP Client:

import java.net.*;
import java.io.*;
public class UDPClient
{
	public static void main (String args[]) throws Exception 
	{
		try
		{
			BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
			
			String M = in.readLine();
			
			byte[] senddata = new byte[1024];
			
			senddata = M.getBytes();
			
			InetAddress ip = InetAddress.getByName("localhost");
			
			DatagramPacket sendPacket = new DatagramPacket(senddata, senddata.length, ip, 20000);
			
			DatagramSocket clientSocket = new DatagramSocket();
			
			clientSocket.send(sendPacket);
			
			byte[] receiveData = new byte[1024];
			
			DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
			
			clientSocket.receive(receivePacket);
			
			String newM = new String(receivePacket.getData());
			
			System.out.println(newM);
		}
		catch (Exception ex)
		{
			System.out.println(ex);
		}
	}//main




}






服务器:






The Server:

import java.io.*; 
import java.net.*; 
class HybridServer implements Runnable
{
	DatagramPacket dgp = null;
	private int port;
	private ServerSocket ss;
	Socket c;
		
	public HybridServer(int i) throws Exception
	{
		port = i;
		ss = new ServerSocket(port);
	}
	
	public HybridServer(DatagramPacket d) throws Exception
	{
		dgp = new DatagramPacket(d.getData(),d.getData().length,d.getAddress(),d.getPort());
	}
	
	public void run()
	{
		 try
		 {
			DatagramSocket ds = new DatagramSocket(dgp.getPort());
			byte[] rData = new byte[1024];
			DatagramPacket d = new DatagramPacket(rData,rData.length);			
			
			while(true)
			{
				ds.receive(d);
				
				if(d.getData().length > 0)
				{
					new UDPThread(d).start();
				}
				else
				{
					c = ss.accept();
				
					new TCPThread(c).start();
				}
				
			}
		 } 
		 catch (Exception ex)
		 {
			
		 }
	}
	
	class TCPThread extends Thread
	{
		public TCPThread(Socket s)
		{
			c = s;
		}
		
		public void run()
		{
			try
			{
				BufferedReader fromClient = new BufferedReader(new InputStreamReader(c.getInputStream())); 
			
				String Message= fromClient.readLine();	
				System.out.println("from client: "+ Message);	
				DataOutputStream toClient= new 	DataOutputStream(c.getOutputStream());
				toClient.writeBytes(Message.toUpperCase()+'\n');
			}
			catch(Exception ex)
			{
			
			}
		}
	}
	
	class UDPThread extends Thread
	{
		public UDPThread(DatagramPacket d)
		{
			port = d.getPort();
			dgp = new DatagramPacket(d.getData(),d.getData().length,d.getAddress(),d.getPort());
		}
		
		public void run()
		{
			try
			{
				String M = new String(dgp.getData());
			
				M = M.toUpperCase();
				
				int port = dgp.getPort();
				
				InetAddress ip = dgp.getAddress();
				
				byte[] sent = new byte[1024];
				
				sent = M.getBytes();
				
				DatagramPacket d = new DatagramPacket(sent, sent.length, ip, port);
				
				DatagramSocket server = new DatagramSocket();
				
				server.send(d);
			}
			catch(Exception ex)
			{
			
			}
		}
	}
}





和线程服务器的类:





And the class that threads the Server:

import java.io.*; 
import java.net.*; 
class ServerStart extends Thread 
{ 
	public static void main(String args[]) throws Exception 
    { 
		byte[] rData = new byte[1024];
			
		InetAddress ip = InetAddress.getByName("localhost");
		
		DatagramPacket d = new DatagramPacket(rData, rData.length, ip, 20000);
		
		new Thread(new HybridServer(10000)).start();
		new Thread(new HybridServer(d)).start();
    }
}

中放入一些sysout, TCP客户端的所有空捕获-我想你会找出失败的原因.

您至少应该将e.printStackTrace()留在那里.最好弄清楚什么时候弹出异常.
put some sysout in that ALL THE empty catch of the TCP-Client - I guess you''ll find out why it fails.

You should at least leave the e.printStackTrace() in there. Better to figure when an exception pops.