// socket for establishing connections

Similar documents
Tutorial on Socket Programming

NETWORK AND SYSTEM PROGRAMMING

CS321: Computer Networks Socket Programming

Introduction to Socket Programming

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

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

Unix Network Programming

CS321: Computer Networks Introduction to Application Layer

System Programming. Sockets

Socket Programming 2007/03/28

CS4514 B08 HELP Session 1

Ken French HELP Session 1 CS4514

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

Hyo-bong Son Computer Systems Laboratory Sungkyunkwan University

A Client-Server Exchange

CS4700/CS5700 Fundamentals of Computer Networking

Socket Programming for TCP and UDP

CS 640: Computer Networking

NETWORK AND SYSTEM PROGRAMMING

Application Programming Interfaces

CS 3516: Computer Networks

1 /* client.c - adapted from code for example client program that uses TCP */ 2 /*Modified by Vincent Chu, Winter

C Structures in Practice

Network Socket Programming - 3 BUPT/QMUL

Socket Programming Example

Network Socket Programming - 3 BUPT/QMUL

Introduction to Network Programming using C/C++

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

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

Network Software Implementations

WinSock. What Is Sockets What Is Windows Sockets What Are Its Benefits Architecture of Windows Sockets Network Application Mechanics

ECE 435 Network Engineering Lecture 2

Network Programming in C: The Berkeley Sockets API. Networked Systems 3 Laboratory Sessions

Linux Programming

ECE 435 Network Engineering Lecture 2

CS118 Discussion 1B, Week 1. Taqi Raza BUNCHE 1209B, Fridays 12:00pm to 1:50pm

How to write a Measurement Telnet Server

How to write a Measurement Telnet Server

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

Socket Programming. Joseph Cordina

SOCKETS. COMP750 Distributed Systems

15-213/ Final Exam Notes Sheet Spring 2013!

CLIENT-SIDE PROGRAMMING

Sockets. UNIX-style IPC. Silberschatz, Galvin and Gagne 2005 Msc. Ivan A. Escobar Broitman 2007

Piotr Mielecki Ph. D.

How do we Communicate? Introduction to Unix Network Programming. What does Alice do? What does Bob do? Two simplest networking programs

COMP/ELEC 429/556 Introduction to Computer Networks

Socket Programming TCP UDP

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

UNIX Network Programming. Overview of Socket API Network Programming Basics

Client/Server. Networking Approach.

Network programming(i) Lenuta Alboaie

Network Socket Programming - 2 BUPT/QMUL

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

CS631 - Advanced Programming in the UNIX Environment Interprocess Communication II

UDP CONNECT TO A SERVER

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

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

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

The Socket Interface. Socket and Socket Library

Department of Computer Science

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

EECS122 Communications Networks Socket Programming. Jörn Altmann

CPSC 213. Introduction to Computer Systems. Inter-Process Communication. Unit 2f

The Berkeley Sockets API. Networked Systems Architecture 3 Lecture 4

TCP/IP Sockets in C: Practical Guide for Programmers. Computer Chat. Internet Protocol (IP) IP Address. Transport Protocols. Ports

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

CSE 333 SECTION 6. Networking and sockets

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

Lab 0. Yvan Petillot. Networks - Lab 0 1

Elementary TCP Sockets

Overview. Administrative. * HW# 5 Due next week. * HW# 5 : Any Questions. Topics. * Client Server Communication. * 12.

Message passing systems are popular because they support client-server interactions, where: clients send messages to servers requesting a server.

The User Datagram Protocol

Client software design

Introduction to Socket Programming

Simple network applications using sockets (BSD and WinSock) Revision 1 Copyright Clifford Slocombe

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

CSCI 4210 Operating Systems CSCI 6140 Computer Operating Systems Sample Exam 2 Questions (document version 1.0)

Network Programming in C. Networked Systems 3 Laboratory Sessions and Problem Sets

ECE322 Systems Programming Project 2: Networking with Matrix Multiplication in C Grant Kimes 12/16/15

Introduction to Network Programming in C/C++

Computer Networks Prof. Ashok K. Agrawala

CSC209H Lecture 9. Dan Zingaro. March 11, 2015

TCP Echo Application: Client & Server. TA: Awad A Younis Class: CS457 Fall 2014

What s an API? Do we need standardization?

Introduction to Berkeley Sockets

Socket programming in C

Programming with TCP/IP. Ram Dantu

Topics for this Week

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

Introduction to Berkeley Sockets

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

Sockets. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

Introduction to Socket Programming

Introduction to Berkeley Sockets

CSC Systems Programming Fall Lecture - XV Network Programming - I. Tevfik Ko!ar. Louisiana State University. November 9 th, 2010

Linux Network Programming, Part 1

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

Transcription:

#include<fcntl.h> #include <string.h> #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #include <netdb.h> #include <arpa/inet.h> #define FILELENGTH 511 // This is not right #define DEPTH 1 #define PACKETSIZE 1023 int lsd = 0 ; // socket for establishing connections #define SA SIZE sizeof( struct sockadrr in ) 1

unsigned long atoip( char text ) { unsigned long ip ; int i, t ; i = 0 ; ip = t = 0 ; while( text[i]! = '\0' ) { if( text[i] == '.' ) { ip = (ip 8) + t ; t = 0 ; else t = t 10 + text[i] '0' ; i++ ; return htonl( (ip 8) + t ) ; 2

struct sockaddr in sa ; int rc ; sa = (struct sockaddr in )malloc( SA SIZE ) ; sa sin family = AF INET ; sa sin port = htons(4950) ; sa sin addr.s addr = atoip( "127.0.0.1" ) ; lsd = socket( PF INET, SOCK STREAM, 0 ) ; if( lsd <= 0 ) { perror( "Socket not created" ) ; kill( getpid(), SIGINT ) ; rc = bind( lsd, (struct sockaddr ) sa, SA SIZE ) ; if( rc == 1 ) { perror( "Bind unsuccessful" ) ; kill( getpid(), SIGINT ) ; listen( lsd, 5 ) ; 3

fd set lsmask ; int new client ; struct timeval timeout ; FD ZERO( &lsmask ) ; FD SET( lsd, &lsmask ) ; timeout.tv sec = 0 ; timeout.tv usec = 10000 ; // 10 msec new client = select( lsd+1, &lsmask, NULL, NULL, &timeout ) ; if( new client ) { new client = accept( lsd, NULL, NULL ) ; if( new client == 1 ) { perror( "Accept failed" ) ; kill( getpid(), SIGINT ) ; 4

rc = recv( cls, buffer, 200, 0 ) ; buffer[rc] = '\0' ; // Just in case sscanf( buffer, "%s %s\n%s", user, pwd, dir ) ; FILE PF = fopen( "./.pwd", "rw" ) ; if( PF == NULL ) { PF = fopen( "./.pwd", "w+" ) ; if( PF == NULL ) { perror( "Failed to create./pwd" ) ; kill( getpid(), SIGINT ) ; fprintf( PF, "%s %s\n", user, pwd ) ; else { fseek( PF, 0, SEEK SET ) ; while( (rc = fscanf( PF, "%s %s\n", PU, PP )) > 0 ) if( strcmp( PU, user ) == 0 ) break ; if( rc <= 0 ) { fprintf( PF, "%s %s\n", user, pwd ) ; else if( strcmp( PP, pwd )! = 0 ) kill( getpid(), SIGINT ) ; 5

void backup directory( int cls, char dir ) { char buffer[filelength], file[filelength] ; int size, rc ; while( (rc = recv( cls, buffer, FILELENGTH, 0 )) > 0 ) { buffer[rc] = '\0' ; sscanf( buffer, "%s %d", file, &size ) ; send( cls, "ACK", 4, 0 ) ; copy( cls, file, size ) ; send( cls, "ACK", 4, 0 ) ; close( cls ) ; 6

int start connection() { struct sockaddr in sa ; int rc, sd ; sa = (struct sockaddr in )malloc( SA SIZE ) ; memset( (char )sa, 0, SA SIZE ) ; sa sin family = AF INET ; sa sin port = htons(4950) ; sa sin addr.s addr = htonl( atoip("127.0.0.1") ) ; sd = socket( PF INET, SOCK STREAM, 0 ) ; if( sd <= 0 ) { perror( "Socket not created" ) ; kill( getpid(), SIGINT ) ; rc = connect( sd, (struct sockaddr ) sa, SA SIZE ) ; if( rc == 1 ) { perror( "Connect failed" ) ; kill( getpid(), SIGINT ) ; return sd ; 7

8

TCP Client/Server Typically, the API is a client server interface. Its library covers the functionality of both of them. TCP Client socket() bind() (?) Server socket() bind() listen() connect() [send() + recv()] close() accept() [send() + recv()] close() The server closes not the socket created by socket(), but the one given by accept(). 9

Client socket() connect() WAIT send() recv() WAIT Server socket() bind() listen() accept() { Other clients + WAIT recv() { Other clients + WAIT send() recv() 10

socket() bind() connect() WAIT Client send() recv() WAIT Server socket() bind() listen() accept() { Other clients + WAIT recv() { Other clients + WAIT send() recv() 11

Socket API desc = socket( protocolfamily, type, protocol ) ; protocolfamily: PF INET (for Internet) etc. type: SOCK STREAM, SOCK DGRAM etc. protocol 0 (normally) or a pointer to a struct manufactured by getprotobyname( tcp ) or similar (see /etc/protocols). 12

Assigning a port to a socket returncode = bind( desc, localaddress, addresslength ) ; The second argument is of type (struct sockaddr *: struct sockaddr { short sa family / protocol family / char sa data[14] ; / address / ; The address field is protocol dependent. 13

Protocol description of address field This is the sockaddr structure for TCP: struct sockaddr in { short sin family ; // = AF INET = PF INET u short sin port ; // port number struct in add sin addr ; // IP address - 4 bytes char sin zero[8] ; // nothing ; An IP address INADDR ANY should be used (unless the machine has several IP addresses and we want to restrict incoming messages only to those using one of the addresses). Likewise, INADDR ANY can be used in the port field. 14

Client starts TCP session returncode = connect( sock, server address, server addresslen ) ; 15

Client side sock = socket( protofamily, type, protocol ) ; struct sockaddr in server address ; server address.sin family = AF INET ; // fill the IP address here server address.sin port = htons( SRV TCP PORT ) ; returncode = connect( sock, server address, server addresslen ) ; send( sock, data address, length, flags ) ;... 16

Byte ordering The Internet protocols require numeric values to be passed in a specified byte order which happens to be different than the host ordering of many machines (e.g. Intel). To convert from/to host to/from network order use: u long htonl( u long hostlong ) ; u short htons( u short hostshort ) ; u long ntohl( u long netlong ) ; u short ntohs( u short netshort ) ; These functions always work; for portability, they cannot be omitted. 17

Names and addresses struct hostent gethostbyname( char host ) ; struct getservbyname( char servname, char protocol ) ; 18