TCPSocket Programming
TCPSocket Programming
What is a socket?
An abstract interface provided to the
application programmer
File descriptor, allows apps to read/write to
the network
Allows to processes on remotely
connected computers to talk to each other
Two types of sockets
SOCK_STREAM SOCK_DGRAM
TCP
UDP
connection oriented,
bidirectional no connection
reliable, in-order
unreliable delivery, no
delivery guarantee on the
order
can send/receive
Socket-programming using TCP
Socket: a door between application process and end-end-
transport protocol (UDP or TCP)
TCP service: reliable transfer of bytes from one process
to another
controlled by
controlled by process application
application process
developer
developer socket socket
controlled by TCP with TCP with controlled by
buffers, operating
operating buffers, internet system
system variables variables
host or host or
server server
FTP Web
Server Server
21 80
Transport
Layer
Network
Layer
DLL/Physical
Socket Programming in C
bind()
connect()
listen()
Client Server
send()
accept()
wait for
recv() connection
.. recv() request from next
.
client
send()
close() close()
socket()
int s_listen = socket(family, type, protocol);
family: AF_INET specifies Ipv4
type: SOCK_STREAM, SOCK_DGRAM
protocol: 0 (pseudo, IP ). See /etc/protocols
bind()
bind(s_listen, localAdd, addLength)
Server specifies which port and address it will be
listening to
s_listen: our listening socket descriptor
localAdd: socket address structure
addLength: length of localAdd
Address Structure
struct sockaddr_in {
u_char sin_len; // length of address
u_char sin_family; // family of address
u_short sin_port; // protocol port num
struct in_addr sin_addr; // IP Addr
char sin_zero[8]; // set to zero, used for
padding
};
Address Structure
Declare address structure
struct sockaddr_in sockAdd;
Set family
sockAdd.sin_family = AF_INET;
Set IP address (2 ways)
//specify address to listen to
inet_pton(AF_INET, “127.0.0.1”, &sockAdd.sin_addr.s_addr)
//listen to any local address
sockAdd.sin_addr.s_addr = htonl(INADDR_ANY)
Set port
sockAdd.sin_port = htons(9999);
listen()
int status = listen(s_listen, queuelength);
status: -1 if error, 0 otherwise
s_listen: socket descriptor
queuelength: Number of clients that can “wait” for a connection
listen is non-blocking: returns immediately
accept()
int s_new = accept(s_listen, &clientAddress,
&addLength);
s_new: new socket for communication with client
s_listen: the listening socket
clientAddress: struct sockaddr, address of client
addLength: size of client address structure
accept is blocking: waits for connection before returning
Talking
int send(int s_new, const void *buf, int len, int
flags);
• s_new – socket descriptor
• buf – pointer to buffer
• len – size of buffer
• flags – can be safely set to 0
int recv(int s_new, void *buf, int len, unsigned
int flags);
• similar to send
• buf holds the data to be transferred
System calls - fork()
fork() is a C system call used to spawn child
processes
Execution for both child and parent process
continues at the next instruction
fork() returns
• 0 if this is the child process
• PID (>0) of the child process if this is the parent
• <0 if fork() fails
Used to keep listening on socket and talking
on another socket
Socket Programming in Java
A stream is a sequence of
characters that flow into
or out of a process.
inFromUser
input
stream
An input stream is Client
Process
attached to some input process
source for the process,
e.g., keyboard or socket.
An output stream is
attached to an output
inFromServer
outToServer
output input
source, e.g., monitor or stream stream
socket.
client
clientSocket
TCP
socket TCP
socket
sentence = inFromUser.readLine();
Send line
to server outToServer.writeBytes(sentence + '\n');
clientSocket.close();
}
}
CPSC 441 - Application Layer 20
Example: Java server (TCP)
import java.io.*;
import java.net.*;
class TCPServer {
Create output
stream, attached DataOutputStream outToClient =
to socket new DataOutputStream(connectionSocket.getOutputStream());
Read in line
from socket clientSentence = inFromClient.readLine();