socket programming in linuxweb.iyte.edu.tr/.../courses/ceng312/socketprogramming.pdfhow to make a...

9
Socket Programming in Linux

Upload: others

Post on 17-Sep-2020

0 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Socket Programming in Linuxweb.iyte.edu.tr/.../courses/ceng312/SocketProgramming.pdfHow to Make a Client The system calls for establishing a connection are somewhat different for the

Socket Programming in Linux

Page 2: Socket Programming in Linuxweb.iyte.edu.tr/.../courses/ceng312/SocketProgramming.pdfHow to Make a Client The system calls for establishing a connection are somewhat different for the

Server Types

Iterative Server: This is the simplest form of server where a serverprocess serves one client and after completing the first request, it takesrequest from another client. Meanwhile, another client keeps waiting.

Concurrent Servers: This type of server runs multiple concurrentprocesses to serve many requests at a time because one process maytake longer and another client cannot wait for so long. The simplest wayto write a concurrent server under Unix is to fork a child process to handleeach client separately.

Page 3: Socket Programming in Linuxweb.iyte.edu.tr/.../courses/ceng312/SocketProgramming.pdfHow to Make a Client The system calls for establishing a connection are somewhat different for the

How to Make a Client

● The system calls for establishing a connection are somewhat different for the client and the server, but both involve the basic construct of a socket. Steps are:

● Create a socket with the socket() system call.

● Connect the socket to the address of the server using the connect() system call

● Send and receive data. There are a number of ways to do this, but the simplest way is to use the read() and write() system calls.

Page 4: Socket Programming in Linuxweb.iyte.edu.tr/.../courses/ceng312/SocketProgramming.pdfHow to Make a Client The system calls for establishing a connection are somewhat different for the

How to Make a Server

● The steps involved in establishing a socket on the server side are as follows:

● Create a socket with the socket() system call.

● Bind the socket to an address using the bind() system call. For a server socket on the Internet, an address consists of a port number on the host machine.

● Listen for connections with the listen() system call

● Accept a connection with the accept() system call. This call typically blocks the connection until a client connects with the server.

● Send and receive data using the read() and write() system calls.

Page 5: Socket Programming in Linuxweb.iyte.edu.tr/.../courses/ceng312/SocketProgramming.pdfHow to Make a Client The system calls for establishing a connection are somewhat different for the

Client and Server Interaction

Page 6: Socket Programming in Linuxweb.iyte.edu.tr/.../courses/ceng312/SocketProgramming.pdfHow to Make a Client The system calls for establishing a connection are somewhat different for the

Example Client: client.c#include<stdio.h>#include<string.h> //strlen#include<sys/socket.h>#include<arpa/inet.h> //inet_addr int main(int argc , char *argv[]){ int socket_desc; struct sockaddr_in server; char *message; //Create socket socket_desc = socket(AF_INET , SOCK_STREAM , 0); server.sin_addr.s_addr = inet_addr("74.125.235.20"); server.sin_family = AF_INET; server.sin_port = htons( 80 ); //Connect to remote server connect(socket_desc , (struct sockaddr *)&server , sizeof(server)) < 0); //Send some data message = "GET / HTTP/1.1\r\n\r\n"; if( send(socket_desc , message , strlen(message) , 0) < 0) { puts("Send failed"); return 1; } puts("Data Send\n"); return 0;}

Page 7: Socket Programming in Linuxweb.iyte.edu.tr/.../courses/ceng312/SocketProgramming.pdfHow to Make a Client The system calls for establishing a connection are somewhat different for the

Example Server: server.cint socket_desc , new_socket , c;

struct sockaddr_in server , client; char *message; //Create socket socket_desc = socket(AF_INET , SOCK_STREAM , 0); //Prepare the sockaddr_in structure server.sin_family = AF_INET; server.sin_addr.s_addr = INADDR_ANY; server.sin_port = htons( 8888 );

bind(socket_desc,(struct sockaddr *)&server , sizeof(server)); listen(socket_desc , 3); //Accept and incoming connection puts("Waiting for incoming connections..."); c = sizeof(struct sockaddr_in); new_socket = accept(socket_desc, (struct sockaddr *)&client, (socklen_t*)&c);

puts("Connection accepted"); //Reply to the client message = "Hello Client , I have received your connection. But I have to go now, bye\n"; write(new_socket , message , strlen(message));

Page 8: Socket Programming in Linuxweb.iyte.edu.tr/.../courses/ceng312/SocketProgramming.pdfHow to Make a Client The system calls for establishing a connection are somewhat different for the

Server: Using fork()

printf("server: waiting for connections...\n");

while(1) { // main accept() loop sin_size = sizeof their_addr; new_fd = accept(sockfd, (struct sockaddr *)&their_addr, &sin_size); if (new_fd == -1) { perror("accept"); continue; }

inet_ntop(their_addr.ss_family, get_in_addr((struct sockaddr *)&their_addr), s, sizeof s); printf("server: got connection from %s\n", s);

if (!fork()) { // this is the child process close(sockfd); // child doesn't need the listener if (send(new_fd, "Hello, world!", 13, 0) == -1) perror("send"); close(new_fd); exit(0); } close(new_fd); // parent doesn't need this }

Page 9: Socket Programming in Linuxweb.iyte.edu.tr/.../courses/ceng312/SocketProgramming.pdfHow to Make a Client The system calls for establishing a connection are somewhat different for the

Lab Work: Socket programming in Linux

● Write a chat program chat.c enables any two PCs to chat with eachother.

● Program accepts one argument (target IP) from command line like:

– ./chat 10.1.12.124● When it starts, it behaves as a server and waits for a chat request from

another PC.

● If you press the ENTER button, it tries to connect to the target IP.

● When connection occurs, both chat programs displays “Hello”, then chat begins. Any string entered is sent to the other machine that is displayed.

● During the chat, if any third machine tries to connect to one of the two chatting programs, the chat program sends a message “Sorry I'm Busy”.