- //UDPCLIENT:
- import java.io.*;
- import java.net.*;
- import java.net.DatagramPacket;
- import java.net.DatagramSocket;
- import java.io.IOException;
- import java.net.SocketException;
- class test1
- {
- void receiving()
- {
- try
- {
- DatagramSocket socket=new DatagramSocket(5001);
- byte[] buff=new byte[1024];
- InetAddress address=InetAddress.getByName("localhost");
- int rec=0;
- while(true)
- {
- rec = 0;
- DatagramPacket packet=new DatagramPacket(buff,buff.length);
- socket.receive(packet);
- String client_packet=new String(packet.getData(),0,packet.getLength());
- System.out.println("Him : "+client_packet);
- rec =1;
- if(rec > 0)
- {
- socket.close();
- break;
- }
- }
- }
- catch(SocketException socket_error)
- {
- System.out.println("Socket exception has occured.");
- socket_error.printStackTrace();
- }
- catch(IOException input_error)
- {
- System.out.println("Input error has occured.");
- input_error.printStackTrace();
- }
- }
- void sending()
- {
- try
- {
- DatagramSocket socket=new DatagramSocket();
- byte [] buff=new byte[1024];
- InetAddress address=InetAddress.getByName("localhost");
- // System.out.println("Trying...to connect to the server.");
- while(true)
- {
- System.out.print("You : ");
- BufferedReader buff_data=new BufferedReader(new InputStreamReader(System.in));
- String user_data=buff_data.readLine();
- System.out.println();
- buff=user_data.getBytes();
- DatagramPacket packet=new DatagramPacket(buff,buff.length,address,4455);
- socket.send(packet);
- break;
- }
- }
- catch(UnknownHostException unknown_host)
- {
- System.out.println("Unknown host..");
- unknown_host.printStackTrace();
- }
- catch(SocketException socket_error)
- {
- System.out.println("Some socket error has occured.");
- socket_error.printStackTrace();
- }
- catch(IOException input_error)
- {
- System.out.println("Some input error has occured.");
- input_error.printStackTrace();
- }
- }
- }
- class UDPC1
- {
- public static void main(String args[])
- {
- int count = 0;
- test1 t = new test1();
- while(true)
- {
- if(count == 0)
- { t.sending();
- // System.out.println(count);
- count=1;
- }
- if(count == 1)
- { t.receiving();
- // System.out.println(count);
- count=0;
- }
- }
- }
- }
- //UDPSERVER
- import java.net.DatagramPacket;
- import java.net.DatagramSocket;
- import java.io.IOException;
- import java.net.SocketException;
- import java.io.*;
- import java.net.*;
- class test2
- {
- void receiving()
- {
- try
- {
- DatagramSocket socket=new DatagramSocket(4455);
- byte[] buff=new byte[1024];
- InetAddress address=InetAddress.getByName("localhost");
- int rec = 0;
- while(true)
- {
- rec = 0;
- DatagramPacket packet=new DatagramPacket(buff,buff.length);
- socket.receive(packet);
- String client_packet=new String(packet.getData(),0,packet.getLength());
- System.out.println("Him : "+client_packet);
- rec =1;
- if(rec > 0)
- {
- socket.close();
- break;
- }
- }
- }
- catch(SocketException socket_error)
- {
- System.out.println("Socket exception has occured.");
- socket_error.printStackTrace();
- }
- catch(IOException input_error)
- {
- System.out.println("Input error has occured.");
- input_error.printStackTrace();
- }
- }
- void sending()
- {
- try
- {
- DatagramSocket socket=new DatagramSocket();
- byte [] buff=new byte[1024];
- InetAddress address=InetAddress.getByName("localhost");
- // System.out.println("Trying...to connect to the server.");
- while(true)
- {
- System.out.print("You : ");
- BufferedReader buff_data=new BufferedReader(new InputStreamReader(System.in));
- String user_data=buff_data.readLine();
- System.out.println();
- buff=user_data.getBytes();
- DatagramPacket packet=new DatagramPacket(buff,buff.length,address,5001);
- socket.send(packet);
- break;
- }
- }
- catch(UnknownHostException unknown_host)
- {
- System.out.println("Unknown host..");
- unknown_host.printStackTrace();
- }
- catch(SocketException socket_error)
- {
- System.out.println("Some socket error has occured.");
- socket_error.printStackTrace();
- }
- catch(IOException input_error)
- {
- System.out.println("Some input error has occured.");
- input_error.printStackTrace();
- }
- }
- }
- class UDPS1
- {
- public static void main(String args[])
- {
- int count = 0;
- test2 t = new test2();
- while(true)
- {
- if(count == 0)
- { t.receiving();
- // System.out.println(count);
- count=1;
- }
- if(count == 1)
- { t.sending();
- // System.out.println(count);
- count=0;
- }
- }
- }
- }
4887 characters | 229 lines | 4.77 KB
