NP Sockets
NP Sockets
Application
Network API
3
4 Data structure for file 2
local
remote
application program
stream datagram
socket socket raw
interface interface socket
interface
TCP UDP
IP
host byte
16-bit order 32-bit
network byte
16-bit order 32-bit
inet_ntoa inet_aton
robster.sa_family = AF_DAVESKIDS;
robster.sa_data[0] = 3;
uint16_t htons(uint16_t);
uint16_t ntohs(uint_16_t);
uint32_t htonl(uint32_t);
uint32_t ntohl(uint32_t);
sin_addr
sa_data
sin_zero
mysock = socket(PF_INET,SOCK_STREAM,0);
myaddr.sin_family = AF_INET;
myaddr.sin_port = htons( portnum );
myaddr.sin_addr = htonl( ipaddress);
myaddr.port = htons(0);
• Connectionless (UDP)
– send()
– recv()
Netprog: Sockets API
49
Threads
• Threads are lightweight process which share the process
instructions , global variables , open files , signal handlers and
signal dispositions , current working directory and user and
group Ids
• Each thread has its own thread ID , set of Registers, PC and
Stack pointer, stack , errno ,signal mask , priority
• Basic Thread Functions : Creation and Termination
• Int pthread_create(pthread_t *tid,const pthread_attr_t *attr,void
*(*func)(void*), void *arg);
• Int pthread_join(pthread_t tid,void **status); // Wait for a thread
to terminate
• pthread_t pthread_self(void); //Returns thread ID of calling
thread
• Int pthread_detach(pthread_t pid);//We cannot wait for it to
terminate, On its termination all resources are released
• Void pthread_exit(void *status);
• Threads/tcpserv01.c
Netprog: Sockets API
50
Thread Synchronization
• Shared data can be protected using mutex locks provided by
the pthread library
• int pthread_mutex_lock(pthread_mutex_t *mptr);
• int pthread_mutex_unlock(pthread_mutex_t *mptr);
• Threads/example02.c
• Can use condition variables for signalling mechanism
• int pthread_cond_wait(pthread_cond_t *cptr,pthread_mutex_t
*mptr);
• int pthread_cond_signal(pthread_cond_t *cptr);
• Can also use System semaphores from <semaphores.h> See
manual
~syrotiuk/pthreads.cc
serv_sock_desc=socket(AF_INET, SOCK_STREAM,0);
serv_addr.sin_family=AF_INET;
result=inet_aton(“127.0.0.1”, &serv_addr.sin_addr);
if (result==0) {
perror(“inet_aton error”);
exit(0);
}
serv_addr.sin_port=htons(4242);
serv_len=sizeof(serv_addr); Netprog: Sockets API
bind(serv_sock_desc, (struct sockaddr *)&serv_addr, serv_len); 62
Basic Socket API in C – cont.
a socket to a port address
FORMAT: int bind(int serv_sock_desc,(struct sockaddr *)&serv_addr, int
serv_len)
The bind function takes three arguments:
– The socket descriptor returned by an earlier call to socket
– The address of the sockaddr structure holding the name
information
– The length of that name structure
The name information in the sockaddr structure
includes:
– domain (AF_INET)
– internet address of the socket (127.0.0.1)
– port number to be bound to the socket, we have chosen port
number 4242 in theNetprog: Sockets API
lab. session
63
Basic Socket API in C – cont.
Create a queue for client request –server code
– The listen call creates a queue for holding
pending clinet requests.
listen(serv_sock_desc,1);
The two arguments to listen are
– The socket descriptor for the socket being
listened to (serv_sock_dec)
– The queue length desired – arbitrary, we’ll use 1
for our example, but something like 5 would be
more typical
char ch;
read(cli_sock_desc, &ch, 1);
write(cli_sock_desc, &ch, 1);