Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
4 views2 pages

61FIT3NPR - W08 Tut UDP Socket New

Download as docx, pdf, or txt
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1/ 2

Faculty of Information Technology

HANOI UNIVERSITY

61FIT3NPR – Network Programming


Tutorial week 08
Java UDP Socket
1. Exercise 1: UDP socket

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

class UDPEchoServer {
public static void main(String args[]) throws Exception {
int port = 9876;
DatagramSocket serverSocket = new DatagramSocket(port);
System.out.println("Server is running...");
//byte[] receiveData = new byte[1024];
//byte[] sendData = new byte[1024];

while(true) {
byte[] receiveData = new byte[1024];
byte[] sendData = new byte[1024];
//Tạo 1 gói dữ liệu sẵn sàng nhận dữ liệu(2)(đầu vào là
biến lưu dữ liệu dạng byte trống & chiều dài)
DatagramPacket receivePacket = new DatagramPacket (receiveData,
receiveData.length);
//sau khi chạy dòng này, sẽ chặn thực thi cho đến khi nhận
được 1 gói packet gửi xuống -> receivePacket != null -> run
serverSocket.receive(receivePacket);
//Nhận message của client trong gói giữ liệu bằng
method .getData()
String sentence = new String(receivePacket.getData());
sentence = sentence.trim();
System.out.println("Message from client: "+sentence);

InetAddress IPAddress = receivePacket.getAddress(); //Điachi IP


int clientPort = receivePacket.getPort();//get port of client
String capitalizedSentence = sentence.toUpperCase();
sendData = capitalizedSentence.getBytes();
//Tạo 1 gói dữ liệu sẵn sàng gửi dữ liệu (4)(đầu vào là
biến lưu dữ liệu dạng byte chứa dữ liệu cần gửi; chiều dài; IP; Port)
DatagramPacket sendPacket = new DatagramPacket (sendData,
sendData.length, IPAddress, clientPort);
// method gửi dữ liệu cho client: .send
serverSocket.send(sendPacket);

}
}
}
import java.io.*;
import java.net.*;

public class UDPClient {


public static void main(String args[]) throws Exception {
BufferedReader inFromUser = new BufferedReader(new
InputStreamReader (System.in));
int port = 9876;
DatagramSocket clientSocket = new DatagramSocket();
InetAddress IPAddress = InetAddress.getByName("localhost");

//byte[] sendData = new byte[1024];


//byte[] receiveData = new byte[1024];

while(true) {
byte[] sendData = new byte[1024];
byte[] receiveData = new byte[1024];
System.out.print("Please enter your message: ");
String sentence = inFromUser.readLine();
System.out.println();
sendData = sentence.getBytes();
DatagramPacket sendPacket = new DatagramPacket (sendData,
sendData.length, IPAddress, port);
clientSocket.send(sendPacket);

DatagramPacket receivePacket = new DatagramPacket


(receiveData, receiveData.length);
clientSocket.receive(receivePacket);
String modifiedSentence = new String(receivePacket.getData());
modifiedSentence = modifiedSentence.trim();
System.out.println("FROM SERVER:" + modifiedSentence);

//clientSocket.close();
}

}
}

2. Exercise 2:
Redo exercise 1 of this tutorial but:
- Client: instead of sending a string to server, client requires user to
enter one real number R and sends it to server, then receives a cube
value of that real number (R3).
- Server: server receives a real number R from client, calculates cube
value of that number (R3) and sends back to client.

3. Exercise 3: Redo the exercise 1 of this Tutorial with multithread server


(usually to redirect the request to another server or share/balance the load of
multiple servers/cores).

You might also like