Socket Programming Example

Similar documents
Ken French HELP Session 1 CS4514

CS321: Computer Networks Socket Programming

Sockets Sockets Communication domains

Socket Programming for TCP and UDP

CS321: Computer Networks Introduction to Application Layer

System Programming. Sockets

// socket for establishing connections

Socket Programming 2007/03/28

Introduction to Socket Programming

Elementary TCP Sockets

Sockets. Dong-kun Shin Embedded Software Laboratory Sungkyunkwan University Embedded Software Lab.

Hyo-bong Son Computer Systems Laboratory Sungkyunkwan University

Chapter 6. The Transport Layer. Transport Layer 3-1

A Client-Server Exchange

CSE 333 Section 8 - Client-Side Networking

Lecture 24. Thursday, November 19 CS 375 UNIX System Programming - Lecture 24 1

Oral. Total. Dated Sign (2) (5) (3) (2)

Unix Network Programming

Lab 0. Yvan Petillot. Networks - Lab 0 1

CSE 124 Discussion Section Sockets Programming 10/10/17

NETWORK PROGRAMMING. Instructor: Junaid Tariq, Lecturer, Department of Computer Science

Project 3. Reliable Data Transfer over UDP. NTU CSIE Computer Networks 2011 Spring

Socket Programming TCP UDP

Client-server model The course that gives CMU its Zip! Network programming Nov 27, Using ports to identify services.

Sockets 15H2. Inshik Song

Types (Protocols) Associated functions Styles We will look at using sockets in C Java sockets are conceptually quite similar

Tutorial on Socket Programming

CS4514 (C04) HELP Session 1 Introduction to Network Programming (v1.3)

CS 3516: Computer Networks

CSC209H Lecture 9. Dan Zingaro. March 11, 2015

STUDY OF SOCKET PROGRAMMING

CSMC 412. Computer Networks Prof. Ashok K Agrawala Ashok Agrawala Set 2. September 15 CMSC417 Set 2 1

CS 640: Computer Networking

Programming with TCP/IP. Ram Dantu

CS 43: Computer Networks. 05: Socket Programming September 12-14, 2018

Group-A Assignment No. 6

The BSD UNIX Socket Interface (CS 640 Lecture) Assignment 1. Interprocess Communication (IPC) Work Individually (no groups)

Hybrid of client-server and P2P. Pure P2P Architecture. App-layer Protocols. Communicating Processes. Transport Service Requirements

SOCKETS. COMP750 Distributed Systems

UNIX Sockets. Developed for the Azera Group By: Joseph D. Fournier B.Sc.E.E., M.Sc.E.E.

Ports under 1024 are often considered special, and usually require special OS privileges to use.

Network Socket Programming - 3 BUPT/QMUL

UDP CONNECT TO A SERVER

BSD Sockets API. Mesut Ali ERGIN. Yeditepe University Information and Computer Science Department

Sockets. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

CS4514 B08 HELP Session 1

Department of Computer Science

UNIX Sockets. COS 461 Precept 1

Lecture 7. Followup. Review. Communication Interface. Socket Communication. Client-Server Model. Socket Programming January 28, 2005

CSE 333 SECTION 6. Networking and sockets

Network Programming Worksheet 2. Simple TCP Clients and Servers on *nix with C.

Networked Applications: Sockets. End System: Computer on the Net

Network Socket Programming - 3 BUPT/QMUL

Introduction to Computer Systems. Networks 2. c Theodore Norvell. The Sockets API

Interprocess Communication Mechanisms

The User Datagram Protocol

Interprocess Communication Mechanisms

Socket Programming. CSIS0234A Computer and Communication Networks. Socket Programming in C

Socket Programming. Dr. -Ing. Abdalkarim Awad. Informatik 7 Rechnernetze und Kommunikationssysteme

Outline. Distributed Computer Systems. Socket Basics An end-point for a IP network connection. Ports. Sockets and the OS. Transport Layer.

C Structures in Practice

Introduction to Socket Programming

Networked Applications: Sockets. Goals of Todayʼs Lecture. End System: Computer on the ʻNet. Client-server paradigm End systems Clients and servers

A Socket Example. Haris Andrianakis & Angelos Stavrou George Mason University

PA #2 Reviews. set_name, get_name, del_name. Questions? Will be modified after PA #4 ~

Christian Tschudin (basierend auf einem Foliensatz von C. Jelger und T. Meyer) Departement Mathematik und Informatik, Universität Basel

Computer Network Lab, SS Fachgebiet Technische Informatik, Joachim Zumbrägel. Overview. Sockets. Sockets in C.

Interprocess Communication

Computer Networks Prof. Ashok K. Agrawala

A. Basic Function Calls for Network Communications

MPAC BENCHMARKING LIBRARY

Internet protocol stack. Internetworking II: Network programming. April 20, UDP vs TCP. Berkeley Sockets Interface.

Socket Programming. #In the name of Allah. Computer Engineering Department Sharif University of Technology CE443- Computer Networks

Introduction to Client-Server Model

EECS122 Communications Networks Socket Programming. Jörn Altmann

Internetworking II: Network programming. April 20, 2000

Network Software Implementations

ECE 435 Network Engineering Lecture 2

Application Programming Interfaces

Network Programming November 3, 2008

Socket Programming. Sungkyunkwan University. Hyunseung Choo Copyright Networking Laboratory

Context. Distributed Systems: Sockets Programming. Alberto Bosio, Associate Professor UM Microelectronic Departement

Outline. Distributed Computing Systems. Socket Basics (1 of 2) Socket Basics (2 of 2) 3/28/2014

The Socket Interface. Socket and Socket Library

SOCKET PROGRAMMING. What is a socket? Using sockets Types (Protocols) Associated functions Styles

Lecture 2. Outline. Layering and Protocols. Network Architecture. Layering and Protocols. Layering and Protocols. Chapter 1 - Foundation

Good Luck! Marking Guide. APRIL 2014 Final Exam CSC 209H5S

UNIX Network Programming. Overview of Socket API Network Programming Basics

Overview. Last Lecture. This Lecture. Daemon processes and advanced I/O functions

TCP: Three-way handshake

Network Socket Programming - 2 BUPT/QMUL

Processes communicating. Network Communication. Sockets. Addressing processes 4/15/2013

UNIX input and output

31 ChamSys Remote Protocol Commands

Server-side Programming

Network Communication

Socket programming in C

NETWORK AND SYSTEM PROGRAMMING

CLIENT-SIDE PROGRAMMING

Transcription:

Socket Programming Example Amir R. Khakpour CSE422 Dept. of Computer Science and Engineering Michigan State University January 25, 2009

What is the problem? Message type=1, Message content= Message type=2, Message content=random_port Message type=3, Message content=client_msg Message type=4, Message content= neg_port random_port client server Two phase communication - Port number negotiation (using UDP sockets) - Message transaction (using TCP socket) Each message has three fields: - message type: integer - message content length: integer - message content: character [128]

Solution Command line executables - Client side:./client -s 35.9.26.200 -p 12014 -m mymessage! - Server side:./server Source Code files - client.cc - server.cc - packet.h struct message packet { int type; int msg len; int port num; char message[128]; };

[client.cc] Taking care of input arguments const char* server host = "127.0.0.1"; unsigned short neg port = 0; const char * MESSAGE=""; if (( argc!= 7 ) && ( argc!= 5 )){ cout << " Usage: " << argv[0] << " -s <hostname> -p <udp port> -m <message>" << endl; exit(0); } else for (int i=1; i<argc; ++i) if (strcmp(argv[i], "-s") == 0) server host = argv[++i]; else if (strcmp(argv[i], "-p") == 0) neg port = (short) atoi(argv[++i]); else if (strcmp(argv[i], "-m") == 0) MESSAGE=argv[++i];

[client.cc] Creating the UDP socket client-side Create a socket and store its descriptor int udp fd; // socket descriptor // create the socket if ( (udp fd = socket(af INET,SOCK DGRAM,0)) < 0 ) { cerr << "udp error!" << endl; exit(0); } Store the server s address in an object the network client can understand sockaddr in server; socklen t slen=sizeof(server); // length of remote address // designate the addressing family server.sin family = AF INET; server.sin port = neg port; hostent *hp = gethostbyname(server host); memcpy(&server.sin addr,hp->h addr, hp->h length); Important note: based on the operating system that you run your program on you may need to use htons() function to parse a number to server.sin port. htonl() and htons() convert long integer and short integer (respectively) from host byte order (on x86 this is Least Significant Byte First) to standard network byte order (Most Significant Byte First).

[server.cc] Creating the UDP socket on the server side Similar to client.cc we have: int udp fd; // udp file descriptor // create the UDP socket if ( (udp fd = socket(af INET,SOCK DGRAM,0)) < 0 ) { cerr << "udp error!" << endl; exit(0); } sockaddr in server udp; // socket address for us socklen t len = sizeof(server udp); // set up the UDP socket server udp.sin family = AF INET; // internet family server udp.sin port = 0; // system set server udp.sin addr.s addr = INADDR ANY; // wild card machine address

[server.cc] Creating the UDP socket on the server side (cont.) We will need to bind the socket we just created to the port // bind the name (address) to a port if ((bind(udp fd,(struct sockaddr *)&server udp, sizeof(server udp))) < 0 ) { cerr << "udp binding Error!" << endl; exit(0); } Then we need to output the port the OS selected for us to use getsockname(udp fd, (struct sockaddr *)&server udp, &len); cout << "Negotiation Port: " << server udp.sin port << endl;

[server.cc] Creating the TCP socket for server Similar to UDP, for TCP socketr generation we need to change SOCK DGRAM to SOCK STREAM to make the socket connection oriented. We will also have to bind the socket to the server port, like we did for the server s UDP socket. //create the TCP Socket int tcp fd; // tcp file descriptor if ( (tcp fd = socket(af INET,SOCK STREAM,0))<0) { cerr << "tcp error!" << endl; exit(0); } sockaddr in server tcp; //setupf the TCP Socket server tcp.sin family = AF INET; // internet family server tcp.sin port = 0; // get the socket port server tcp.sin addr.s addr = INADDR ANY; // wild card machine address if ((bind(tcp fd,(struct sockaddr *)&server tcp, sizeof(server tcp))) < 0) { cerr << "tcp bind error!" << endl; exit(0); } len = sizeof(server tcp); getsockname(tcp fd, (struct sockaddr *)&server tcp, &len);

[client.cc] Sending the first packet Create the packet using packet.h message packet rqst packet; rqst packet.type=1; rqst packet.msg len=0; bzero(rqst packet.message,128); To send the packet to the server we use command sendto(). It takes the file descriptor, the address of the packet, the size of the packet, and flag (typically always zero), the pointer to the server socket address, and the size of the address. sendto(udp fd,&rqst packet,sizeof(rqst packet),0,(struct sockaddr *)&server, slen); cout <<">> [UDP] Sent: Message Type: "<< rqst packet.type<<" Message Length: " <<rqst packet.msg len<<" Message Content: "<< rqst packet.message <<endl;

[server.cc] Receiving the first packet To receive from the client we use the function recvfrom(), which takes a file descriptor, the pointer to where to store the message, the max size received, a flag (typically zero), a pointer to a socket address for the sender, and the size of address as arguments. We will want to store the address for the client to respond back with the TCP port. sockaddr in client; // socket address for client socklen t clen = sizeof(client); message packet rqst packet; //receive connect request int mesglen = recvfrom(udp fd,&rqst packet,sizeof(rqst packet),0, (struct sockaddr *)&client, &clen); cout <<"<< [UDP] Received: Message Type: "<< rqst packet.type <<" Message Length: " <<rqst packet.msg len <<" Message Content: "<< rqst packet.message <<endl;

[server.cc] Responding to the client Once the server side receives the message it creates a message with type=2 and fill the content by TCP port number. message packet response packet; response packet.type=2; ostringstream random port; random port << server tcp.sin port; bzero(response packet.message,128); response packet.msg len=random port.str().length(); strncpy(response packet.message,random port.str().c str(), response packet.msg len); Send back the response to the client. In the same command we used for sending to the server: sendto(udp fd, &response packet, sizeof(response packet), 0,(struct sockaddr *)&client, clen); cout <<">> [UDP] Sent: Message Type: "<< response packet.type <<" Message Length: " <<response packet.msg len <<" Message Content: "<< response packet.message <<endl; We are done with the UDP file descriptor so close it. close(udp fd);

[client.cc] Receiving the response Like we did with the server we need to receive the packet being sent to us. message packet response packet; int mesglen = recvfrom(udp fd, &response packet,sizeof(response packet),0, (struct sockaddr *)&server, &slen); And like the server we are done with the file descriptor. close(udp fd); And we print out the received packet. char out1[128]=""; strncpy(out1,response packet.message,response packet.msg len); cout <<"<< [UDP] Received: Message Type: "<< response packet.type <<" Message Length: "<<response packet.msg len <<" Message Content: "<< out1 <<endl; cout << "So the Random port (random port) is: "<< out1 << endl;

[client.cc] Creating the TCP socket for client Just like the server we will need to create a socket for the TCP connection. int tcp fd; if ( (tcp fd = socket(af INET, SOCK STREAM, 0)) < 0) { cerr << "tcp error!" << endl; exit(0); }

[server.cc] Wait for a connection request and accept Now that we have a socket and the client matches the username, we need to listen for their connection and accept the connection. Also we should close the first TCP file descriptor because we no longer are listening for connections. listen(tcp fd, 1); int acc tcp fd = accept(tcp fd, (struct sockaddr *)0, (socklen t *)0); close(tcp fd); Make sure the file descriptor exists, otherwise quit. if(acc tcp fd == -1) { cout << "accept failure" << endl; exit(0); }

[client.cc] Connecting to the server We update the port number by the random port and then connect to the server with the address and port we received the response packet from. // update the server socket port number server.sin port = atoi(out1); if(connect(tcp fd, (struct sockaddr*)&server, slen) < 0) { cout << "connection error" << endl; close(tcp fd); exit(0); }

[client.cc] Sending a message to the server Now that we are connected, let s send messages to the server. char out2[128]=""; message packet client msg; client msg.type=3; client msg.msg len=strlen(message); bzero(client msg.message,128); strncpy(client msg.message,message,client msg.msg write(tcp fd, &client msg, sizeof(client msg)); cout <<">> [TCP] Sent: Message Type: "<< client msg.type <<" Message Length: " <<client msg.msg len <<" Message Content: "<< MESSAGE <<endl;

[server.cc] Receiving a message from client message packet client msg; // receive the message mesglen = read(acc tcp fd, &client msg, sizeof(client msg)); cout <<"<< [TCP] Received: Message Type: "<< client msg.type <<" Message Length: " <<client msg.msg len <<" Message Content: "<< client msg.message <<endl;

[server.cc] Sending a message to the client The server writes on the socket and the client will read that message packet rcv msg; rcv msg.type=4; rcv msg.msg len=0; write(acc tcp fd, &rcv msg, sizeof(rcv msg)); char out3[128]=""; strncpy(out3,rcv msg.message,rcv msg.msg len); cout <<">> [TCP] Sent: Message Type: "<< rcv msg.type <<" Message Length: " <<rcv msg.msg len <<" Message Content: "<< out3 <<endl;

[client.cc] Receiving a message from server char out3[128]=""; message packet rcv msg; read(tcp fd, &rcv msg, sizeof(rcv msg)); strncpy(out3,rcv msg.message,rcv msg.msg len); cout <<"<< [TCP] Received: Message Type: "<< rcv msg.type <<" Message Length: " <<rcv msg.msg len <<" Message Content: "<< out3 <<endl;

Cleaning up We need to close all the open file descriptors before we exit. server.cc: // close the socket close(acc tcp fd); client.cc: close(tcp fd);