09 April 2016

UDPServer UDPClient

Converting String to Int in Java?

Integer x = Integer.valueOf(str);
// or
int y = Integer.parseInt(str);

int foo = Integer.parseInt("1234");
UDPServer UDPClient Servlet 
package demo5;

import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;

public class UDPServer {

 public static void main(String[] args) {
  try {
   DatagramSocket serverSocket = new DatagramSocket(9876);
   System.out.println("Server Ready");
   byte[] inFromClient = new byte[1024];
   int lengh = inFromClient.length;

   // Recevice from Client
   DatagramPacket fromClient = new DatagramPacket(inFromClient, 0, lengh);
   serverSocket.receive(fromClient);

   String dataReceived = new String(fromClient.getData(), 0, fromClient.getLength());

   System.out.println("Nhan " + dataReceived);
   dataReceived = " Xin chao " + dataReceived;

   // send to client
   byte[] outToClient = dataReceived.getBytes();
   InetAddress address = fromClient.getAddress();
   int port = fromClient.getPort();
   DatagramPacket toClient = new DatagramPacket(outToClient, outToClient.length, address, port);
   serverSocket.send(toClient);
   serverSocket.close();

  } catch (Exception e) {
   // TODO: handle exception
   e.printStackTrace();
  }
 }
}


package demo5;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;

public class UDPClient {

 public static void main(String[] args) {

  try {
   DatagramSocket clientSocket = new DatagramSocket();
   // Send to Server
   String str = " ABC";
   byte[] outToServer = str.getBytes();
   InetAddress address = InetAddress.getByName("localhost");
   int port = 9876;
   DatagramPacket toServer = new DatagramPacket(outToServer, outToServer.length, address, port);
   clientSocket.send(toServer);
   // Read to Server
   byte[] inFromServer = new byte[1024];
   DatagramPacket fromServer = new DatagramPacket(inFromServer, inFromServer.length);
   clientSocket.receive(fromServer);
   String data = new String(fromServer.getData(), 0, fromServer.getLength());
   System.out.println(data);
   clientSocket.close();
  } catch (IOException e) {
   e.printStackTrace();
  }

 }
}
package demo5;

import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;

public class UDPServer {

 public static void main(String[] args) {
  try {
   DatagramSocket serverSocket = new DatagramSocket(9876);
   System.out.println("Server Ready");
   byte[] inFromClient = new byte[1024];
   int lengh = inFromClient.length;

   // Recevice from Client A
   DatagramPacket fromClient = new DatagramPacket(inFromClient, 0, lengh);
   serverSocket.receive(fromClient);
   String dataReceived = new String(fromClient.getData(), 0, fromClient.getLength());

   // Recevice from Client B
   DatagramPacket fromClientb = new DatagramPacket(inFromClient, 0, lengh);
   serverSocket.receive(fromClientb);
   String dataReceivedb = new String(fromClientb.getData(), 0, fromClientb.getLength());

   int a = Integer.parseInt(dataReceived);
   int b = Integer.parseInt(dataReceivedb);
   int c = a+b;
   dataReceived ="Tong: "+c;

   // send to client A
   byte[] outToClient = dataReceived.getBytes();
   InetAddress address = fromClient.getAddress();
   int port = fromClient.getPort();
   DatagramPacket toClient = new DatagramPacket(outToClient, outToClient.length, address, port);
   serverSocket.send(toClient);

   serverSocket.close();

  } catch (Exception e) {
   // TODO: handle exception
   e.printStackTrace();
  }
 }
}
package demo5;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.util.Scanner;

public class UDPClient {

 public static void main(String[] args) {
  Scanner s = new Scanner(System.in);
  System.out.println("Nhap a: ");
  String str = s.nextLine();
  System.out.println("Nhap b: ");
  String strb = s.nextLine();
  
  try {
   DatagramSocket clientSocket = new DatagramSocket();
   // sell
   InetAddress address = InetAddress.getByName("localhost");
   int port = 9876;
   //A
   byte[] outToServer = str.getBytes();
   DatagramPacket toServer = new DatagramPacket(outToServer, outToServer.length, address, port);
   clientSocket.send(toServer);
   //B
   byte[] outToServerb = strb.getBytes();
   DatagramPacket toServerb = new DatagramPacket(outToServerb, outToServerb.length, address, port);
   clientSocket.send(toServerb);
   // Read to Server
   byte[] inFromServer = new byte[1024];
   DatagramPacket fromServer = new DatagramPacket(inFromServer, inFromServer.length);
   clientSocket.receive(fromServer);
   String data = new String(fromServer.getData(), 0, fromServer.getLength());
   
   System.out.println(data);
   clientSocket.close();
  } catch (IOException e) {
   e.printStackTrace();
  }

 }
}

0 nhận xét:

Post a Comment

 

BACK TO TOP

Xuống cuối trang