Bahir Dar University
Faculty of Computing, Advanced programming with Java
Exercises on Networking in Java
1. [Client-Server Communication using TCP]. Write the following code and try to understand the code.
import [Link]; import [Link];
import [Link]; import [Link];
import [Link]; import [Link];
import [Link];
public class WishesClient public class WishesServer
{ {
public static void main(String args[]) public static void main(String args[]) throws
throws Exception Exception
{ {
Socket sock = new Socket("[Link]", ServerSocket sersock = new ServerSocket(5000);
5000); [Link]("server is ready");
String message1 = "Accept Best Wishes,
Serverji"; Socket sock = [Link]();
OutputStream ostream = [Link](); InputStream istream = [Link]();
DataOutputStream dos = new DataOutputStream(ostream); DataInputStream dstream = new DataInputStream(istream);
[Link](message1);
[Link](); String message2 = [Link]();
[Link](); [Link](message2);
[Link](); dstream .close(); [Link](); [Link]();
} [Link]();
} }
} }
2. [Create a simple network application using TCP]. The client should read two numbers from keyboard and send them to the server.
The server computes the sum and the product of these two numbers and sends the result back to the client. Finally, the client should
display the result on the screen.
Prepared by: Hagos Tesfahun. Page 1
Bahir Dar University
Faculty of Computing, Advanced programming with Java
Exercises on Networking in Java
3. [Develop a client/server GUI based chatting application using TCP]. Interface of both the client and the server program should
have a text area -- where chatting statements of both side will be displayed, a text field -- an area where chatting words are written
on, and a button to send the chatting information, as shown here below.
Prepared by: Hagos Tesfahun. Page 2
Bahir Dar University
Faculty of Computing, Advanced programming with Java
Exercises on Networking in Java
4. [Client-Server Communication using UDP]. Write the following code and try to understand the code.
import [Link].*; import [Link].*;
import [Link].*; import [Link].*;
public class UDPServer { import [Link];
public static void main(String[] args) throws public class UDPClient {
IOException { public static void main(String[] args) throws
DatagramSocket serverSocket = new IOException {
DatagramSocket(5000); DatagramPacket sendPacket;
[Link]("Server is ready"); byte[] sendData;
byte[] receiveData = new byte[1024]; DatagramSocket clientSocket = new DatagramSocket();
DatagramPacket receivePacket; Scanner input = new Scanner([Link]);
while (true) { while (true) {
// Server waiting for clients message [Link]("Enter Message to the Server ");
receivePacket = new DatagramPacket(receiveData, String cmd = [Link]();
[Link]); sendData = [Link]();
[Link](receivePacket); sendPacket = new DatagramPacket(sendData,
// Convert Byte Data to String [Link],
String clientMessage = new [Link]("[Link]"), 5000);
String([Link](),0, [Link](sendPacket);
[Link]()); }
[Link]("Message from Client: " + }
clientMessage); }
}
}
}
Prepared by: Hagos Tesfahun. Page 3