Вы находитесь на странице: 1из 5

North South University

CSE 338 L: Data


Communication & Network Lab
Lab Class 2: Introduction to Socket Programing
_____________________________________________________________________________________

What is UDP?

What is TCP?

How are they different?

Socket Programing
Networking is tightly integrated in Java. Java API provides the classes for creating sockets to facilitate
program communications over the Internet. Sockets are the endpoints of logical connections between
two hosts and can be used to send and receive data. Java treats socket communications much as it
treats I/O operations; thus programs can read from or write to sockets as easily as they can read from or
write to files. Java supports both TCP and UDP for communication.

To establish a server, you need to create a server socket and attach it to a port, which is where
the server listens for connections.

Note:
When you create a server socket, you have to specify a port (e.g., 8000) for the socket. When a
client connects to the server (line 43 in Client.java), a socket is created on the client. This socket

Prepared by Farah Hossain


has its own local port. This port number (e.g., 2047) is automatically chosen by the JVM.

Exercise 1: Create a simple Client


// A Java program for a Client
import java.net.*;
import java.io.*;

public class Client


{
// initialize socket and input output streams
private Socket socket = null;
private DataInputStream input = null;
private DataOutputStream out = null;

// constructor to put ip address and port


public Client(String address, int port)
{
// establish a connection
try
{
socket = new Socket(address, port);
System.out.println("Connected");

// takes input from terminal


input = new DataInputStream(System.in);

// sends output to the socket


out = new DataOutputStream(socket.getOutputStream());
}
catch(UnknownHostException u)
{
System.out.println(u);
}
catch(IOException i)
{
System.out.println(i);
Prepared by Farah Hossain
}

// string to read message from input


String line = "";

// keep reading until "Over" is input


while (!line.equals("Over"))
{
try
{
line = input.readLine();
out.writeUTF(line);
}
catch(IOException i)
{
System.out.println(i);
}
}

// close the connection


try
{
input.close();
out.close();
socket.close();
}
catch(IOException i)
{
System.out.println(i);
}
}

public static void main(String args[])


{
Client client = new Client("127.0.0.1", 5000);
}
}

Exercise 2: Create a Server


// A Java program for a Server
import java.net.*;
import java.io.*;

public class Server


{
//initialize socket and input stream
private Socket socket = null;
private ServerSocket server = null;
private DataInputStream in = null;

// constructor with port


public Server(int port)
Prepared by Farah Hossain
{
// starts server and waits for a connection
try
{
server = new ServerSocket(port);
System.out.println("Server started");

System.out.println("Waiting for a client ...");

socket = server.accept();
System.out.println("Client accepted");

// takes input from the client socket


in = new DataInputStream(
new BufferedInputStream(socket.getInputStream()));

String line = "";

// reads message from client until "Over" is sent


while (!line.equals("Over"))
{
try
{
line = in.readUTF();
System.out.println(line);

}
catch(IOException i)
{
System.out.println(i);
}
}
System.out.println("Closing connection");

// close connection
socket.close();
in.close();
}
catch(IOException i)
{
System.out.println(i);
}
}

public static void main(String args[])


{
Server server = new Server(5000);
}
}

Task:
Perform all exercises in the class.

Prepared by Farah Hossain


Home Task:
Write a socket program which maintains constant chat/ exchanges constant messages from both side
until a specific word is used by the client, like ‘Over’ in this program.

Reading References:
1. http://www.oracle.com/technetwork/java/socket-140484.html
2. https://docs.oracle.com/javase/tutorial/networking/sockets/
3. https://docs.oracle.com/javase/7/docs/api/java/net/ServerSocket.html
4. http://www.geeksforgeeks.org/socket-programming-in-java/
5. https://netbeans.org/downloads/

Prepared by Farah Hossain

Вам также может понравиться