Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Socket Programming With UDP and TCP

Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 19

Socket programming with UDP

and TCP
Socket Programming with TCP

• Connection oriented
– Handshaking procedure
• Reliable byte-stream
TCP-client in Java
import java.io*;
import java.net.*;
Class TCPClient {
public static void main (String argv[]) throws Exception {
String sentence;
String modifiedSentence;
BufferedReader inFromUser = new BufferedReader(
new InputStreamReader(system.in));
Socket clientSocket = new Socket(”hostname”, 6789);
DataOutpuStream outToServer = new DataOutputStream(
clientSocket.getOutputStream());
BufferedReader inFromServer =
new BufferedReader(new InputStreamReader(
clientSocket.getInputStream()));
sentence = inFromUser.readLine();
outToServer.writeBytes(sentence + ’\n’);
modifiedSentence = inFromServer.readLine();
System.out.println(”FROM SERVER: ” + modifiedSentence);
clientSocket.close(); } }
TCP-client in Java
import java.io*;
import java.net.*;

• Imports needed packages


Class TCPClient {
public static void main (String argv[]) throws Exception {

• Standard Java initiation


TCP-client in Java
String sentence;
String modifiedSentence;

• Declares two string objects


BufferedReader inFromUser = new BufferedReader(
new InputStreamReader(system.in))

• Creates a stream that handels input from the


user
TCP-client in Java
Socket clientSocket = new Socket(”hostname”, 6789) ;

• Initiate a TCP-connection with the ”hostname”


through port 6789
• Client performes a DNS lookup to obtain host
IP.
DataOutpuStream outToServer = new DataOutputStream(
clientSocket.getOutputStream())

• Creates a stream that handels output to


server
TCP-client in Java
BufferedReader inFromServer =
new BufferedReader(new InputStreamReader(
clientSocket.getInputStream()));

• Creates a stream that handels input from


server
sentence = inFromUser.readLine()

• Puts the input from user into string object


TCP-client in Java
outToServer.writeBytes(sentence + ’\n’);

• Transform sentence to bytes & sends to server


modifiedSentence = inFromServer.readLine();

• Puts input from server into modified sentence


System.out.println(”FROM SERVER: ” + modifiedSentence);
clientSocket.close(); } }

• Prints modifiedSentence and closes the


connection
TCP-server in Java
import java.io*;
import java.net.*;
Class TCPServer {
public static void main (String argv[]) throws Exception {
String clientSentence;
String capitalizedSentence;
ServerSocket welcomeSocket = new ServerSocket (6789);
while (true) {
Socket connenctionSocket = welcomeSocket.accept();
BufferedReader inFromClient = new BufferedReader( new InputStreamReader(
connectionSocket.getInputStream()));
DataOutpuStream outToClient = new DataOutputStream(
connectionSocket.getOutputStream());
clientSentence = inFromClient.readLine();
capitalizedSentence = clientSentence.toUpperCase() + ’\n’;
outToClient.writeBytes(capitalizedSentence);
} }}
TCP-server in Java
ServerSocket welcomeSocket = new ServerSocket (6789);

• Creates a welcomeSocket that handels


connection-attempts from port 6789

Socket connenctionSocket = welcomeSocket.accept();

• Creates a new socket


TCP-server in Java

capitalizedSentence = clientSentence.toUpperCase() + ’\n’;

• This command is the essence of the


application.
Socket Programming with UDP

• Connectionless
• No reliable datatransfer
UDP-client in Java
import java.io.*;
import java.net.*;
class UDPClient {
public static void main(String args[ ]) throws Exception {
BufferedReader inFromUser = new BufferedReader(new InputStreamReader(System.in));
DatagramSocket clientSocket = new DatagramSocket();
InetAddress IPAddress = InetAddress.getByName(”hostname”);
byte[ ] sendData = new byte[1024];
byte[ ] recieveData = new byte[1024];
String sentence = inFromUser.readLine();
sendData = sentence.getBytes ();
DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, IPAddress, 9876);
clientSocket.send(sendPacket);
DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
clientSocket.receive(receivePacket);
String modefiedSentence = new String(receivePacket.getData());
System.out.println(”FROM SERVER:” + modifiedSentence);
clientSocket.close();
} }
UDP-client in Java
DatagramSocket clientSocket = new DatagramSocket();

• This line does not initiate a TCP connection


InetAddress IPAddress = InetAddress.getByName(”hostname”);

• Uses DNS lookup to find the IP-address for


”hostname”
UDP-client in Java
byte[ ] sendData = new byte[1024];
byte[ ] recieveData = new byte[1024];

• Arrays that contains bytes that will be sent


and recieved
DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length,
IPAddress, 9876);

• Creates a packages that containes travel


information
UDP-client in Java
clientSocket.send(sendPacket);

• Sends the package through the client socket


DatagramPacket receivePacket = new DatagramPacket(receiveData,
receiveData.length);

• Creates a placeholder for the package


clientSocket.receive(receivePacket);

• Rececives the package from the server


UDP-client in Java
String modefiedSentence = new String(receivePacket.getData());

• Extracts the data from the package and puts it


in a string package
clientSocket.close();

• Closes the client socket


UDP-server in Java
import java.io.*;
import java.net.*;
class UDPClient {
public static void main(String args[ ]) throws Exception {
DatagramSocket serverSocket = new DatagramSocket(9876);
byte[ ] receiveData = new byte[1024];
byte[ ] sendData = new byte[1024];
while(true) {
DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
serverSocket.receive(receivePacket);
String sentence = new String(receivePacket.getData());
InetAddress IPAddress = receivePacket.getAddress();
int port = receivePacket.getPort();
String catitalizedSentence = sentence.toUpperCase();
sendData = capetalizedSentence.getBytes ();
DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, IPAddress, port);
clientSocket.send(sendPacket);
DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
serverSocket.send(sendPacket);
} }}
UDP-server in Java
DatagramSocket serverSocket = new DatagramSocket(9876);

• Creates a socket at port 9876 that data passes


through
String sentence = new String(receivePacket.getData());
InetAddress IPAddress = receivePacket.getAddress();
int port = receivePacket.getPort();

• Extracts the data and client information from


the package

You might also like