0% found this document useful (0 votes)
4 views20 pages

5-COE312 - Socket Programming

The document provides an overview of socket programming, focusing on the communication between client and server machines using TCP and UDP protocols. It explains the structure and functionality of sockets in Java, including examples of server and client implementations. Additionally, it discusses the use of the SensorLog App for collecting sensor data via TCP connections.

Uploaded by

toshionoeiyu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views20 pages

5-COE312 - Socket Programming

The document provides an overview of socket programming, focusing on the communication between client and server machines using TCP and UDP protocols. It explains the structure and functionality of sockets in Java, including examples of server and client implementations. Additionally, it discusses the use of the SensorLog App for collecting sensor data via TCP connections.

Uploaded by

toshionoeiyu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 20

1

Socket
Programming
COE312
Omar Arif

Slides adapted from Dr Imran A. Zualkernan’s slides


2

 Internet programming is about two


machines talking to each other.
 For example, browser is on one
machine communicating with the
Internet google server running on another
machine.
Programming  A Socket is software structure that
serves as an endpoint for sending
and receiving data across the
internet
3
Feature TCP UDP

Connection Connection-
Connectionless
status oriented
Data
Yes No
sequencing
Guaranteed
Yes No
delivery
Retransmissio No
Retransmissio
n of lost retransmission
n of data
packets of lost packets
Protocols for Extensive
Basic error
communicati Error checking error
checking
checking

on Speed
Slower than
UDP
Faster than
TCP

Broadcasting No Yes

Used by Video
Optimal use HTTPS, HTTP, conferencing,
case SMTP, POP, streaming,
FTP, etc DNS, VoIP, etc
4

 Sockets allow the programmer to treat a


network connection as just another
stream onto which bytes can be written
and from which bytes can be read.
 Java provides
Sockets  Socket and ServerSocket classes for TCP
connection
 DatagramSocket and DatagramPacket
classes for UDP connection
7

 There are two machines; client and


server.
TCP  The client machine connects to a
Connection: server machine using a ‘socket’
Client-Server  The server machine listens on an
Architecture <ip address, port> and connect with
multiple clients.
TCP Connection Example: 8
Client Server
Architecture:
Each socket server listens on
1. IP address
2. Port
Client soc 23
ket

Listening to
socket port 23
Client
server
et
sock
Client

Each socket client connects using Each server can form socket
1. IP address connections with multiple
2. Port clients.
9
 In other words, servers are like
receptionists who sit by the phone
and wait for incoming calls.

public class Server {


public static void startServer(){
try{
ServerSocket ss=new ServerSocket(6666);
System.out.println("Listening”);

The Socket while(true) {


Socket s=ss.accept();
Server System.out.println("Connection");

DataOutputStream dout=new
DataOutputStream(s.getOutputStream());
dout.writeUTF("Welcome");
}
}catch(Exception e){
System.out.println(e);
}
}
}
10

 To test the server we need a socket


client.
 The easiest client to use is a telnet
telnet program that allows you to interact
with the socket server using the
terminal.
11

 For mac users:


https://osxdaily.com/2018/07/18/get
-telnet-macos/

telnet  For PC users:


https://tweaks.com/windows/48535/
how-to-install-telnet-with-only-one-
command/
12

public static void main(String[] args) {


// TODO Auto-generated method stub
try{
Socket s=new
Socket("localhost",6666);
DataInputStream din=new
DataInputStream(s.getInputStream());
System.out.println(din.readUTF());
Socket Client s.close();
}catch(Exception e){
System.out.println(e);
}
}
13

 Socket is a nice library on top of


TCP/IP that operates on bytes going
back and forth between two
machines.
 We can use telnet to simulate a
client.
 We can use Java Socket and

Summary ServerSocket to write clients and


servers.
 Application protocols like HTTP can
easily be implemented on top of
sockets because it is a text-based
protocol and is also based on the
request/response model.
14

 You will use the SensorLog App to


gather sensor data from your mobile
phone.

SensorLog  SensorLog App can send data using


TCP.
App  Let us see how we can write a Java
client to collect sensor data from
the SensorLog App.
15

 Collects sensor data from the


phone.
 We can set the App up as a TCP
SensorLog server. TCP client can make a

App connection with the server and


receive sensor data
 The App is the Server.
16
Case Study: Collecting sensor data from SensorLog App

Setup as a TCP Select the sensors


IP and the Port Server you want to read.
17

 Use the ping command to make


sure that your laptop and the
phone are on the same network.

Make sure
you can see
the phone
19

public class TCPClient implements Runnable {

String host = "127.0.0.1";


int port = 8080;

TCPClient(String host, int port){


this.host = host;

TCPClient this.port = port;


Thread t = new Thread(this);
t.start();
}
20

public void run() {


try {
Socket socket = new Socket(host, port);

InputStream input = socket.getInputStream();


InputStreamReader reader = new InputStreamReader(input);

int character;

while ((character = reader.read()) != -1) {

TCP_Client }
System.out.print((char) character);

(contd.) } catch (UnknownHostException ex) {

System.out.println("Server not found: " + ex.getMessage());

} catch (IOException ex) {

System.out.println("I/O error: " + ex.getMessage());


}
}
21

public class Main{

public static void main(String[] args) {


TCP_Client new TCP_Client("192.168.0.178", 2000);

Main }
}
TCP Client showing sensor data from the phone in 22

JSON format.
24

 SensorLog sends back text which is


in a specific format.
SensorLog  You need to parse the text to extract
Output the information you need for your
program.

You might also like