Click here to Skip to main content
15,908,634 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
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:
Java
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 Client:
Java
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:
Java
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:
Java
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();
    }
}
Posted
Comments
Sergey Alexandrovich Kryukov 14-Feb-12 17:57pm    
This is not a question. What's the problem, exactly?
--SA
AR1988 14-Feb-12 18:00pm    
When testing if the client is TCP or UDP its not giving a good result the server is only replying to the UDP client
the code in the server here
"
while(true)
{
ds.receive(d);

if(d.getData().length > 0)
{
new UDPThread(d).start();
}
else
{
c = ss.accept();

new TCPThread(c).start();
}

}
"
I think it has something wrong

PLZ I really need your help I have an exam after a couple of hours and haven't solved this issue yet

1 solution

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.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900