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

Size: px
Start display at page:

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

Transcription

1 Simple network applications using sockets (BSD and WinSock) Revision 1 Copyright Clifford Slocombe sockets@slocombe.clara.net COPYRIGHT CLIFFORD SLOCOMBE PAGE 1 OF 8

2 Table of Contents Introduction...3 Socket APIs...3 Byte order and structure packing...4 Establishing a communication session...4 Client...4 Server...4 A simple example application...5 Client code for BSD/WinSock...5 Server code for BSD/WinSock...7 Practical applications...8 COPYRIGHT CLIFFORD SLOCOMBE PAGE 2 OF 8

3 Introduction While general-purpose network applications, such as telnet and FTP are useful, it is often necessary to implement customised communicating applications. The ability to leverage the security, flexibility, and universality of TCP/IP is extremely valuable. The ability of TCP/IP to support multiple simultaneous communicating applications makes it well suited to multi-threaded applications, and supports the efficient use of available physical communication links. This document describes only the use of TCP sockets, which provide character stream communication. UDP sockets, which provide data packet, or datagram communication, are not discussed, although much of the information contained herein is applicable to both UDP and TCP. The socket API does not specify any particular communications protocol, however it is most commonly used with TCP/IP. TCP/IP is assumed throughout this document. A WinSock TCP/IP application may communicate with a BSD socket TCP/IP application, allowing cross-platform networking. This document is an introduction to building communicating applications using both BSD and Windows socket APIs. It presents a simple client/server application coded to both APIs that demonstrates the principles. Windows sockets (WinSock) is specific to Windows and is similar but not identical to BSD. The BSD socket API has been implemented on many platforms including Linux, and is available for embedded systems and real-time operating systems. Socket APIs BSD compatible socket API are available for many platforms; existing TCP/IP application code for UNIX or Linux platforms may be ported with little of no modification. Windows implements the Windows Sockets API. Windows Sockets is based on, but not entirely compatible with BSD 4.3 sockets. Apart from proprietary initialisation and clean-up functions, needed to support the windows environment, there are a number of important differences between BSD sockets and Windows Sockets, the main ones are as follows: Socket descriptors (or handles) in Windows are of type SOCKET, in BSD they are of type int. This is important for a number of reasons, SOCKET is unsigned, and in BSD the value -1 is used to indicate an error. In Windows, the manifest constant INVALID_SOCKET is used to indicate an error. In BSD, socket descriptors and file descriptors are entirely compatible, allowing the file I/O functions such as read(), and write(), to be used with socket streams. In Windows this is not guaranteed to be the case, so the socket specific equivalents such as recv(), and send(), should be used. In the example code I have used socket specific functions for both the BSD and WinSock applications. This keeps things simpler and makes the porting of code between the two platforms simpler. Because the Windows Sockets API is not compatible with the file I/O API, two functions common to both I/O and sockets in BSD have been renamed in Windows. These are close(), and ioctl(), called closesocket(), and ioctlsocket (), respectively in Windows. Code that does not strictly use the FD_XXX macros for accessing fd_set structures will not work correctly with the select() function. Windows Sockets error codes are not available via the errno variable. The function WSAGetLastError() returns socket error codes. BSD error code manifest constants are defined for compatibility, but Microsoft recommend that the WSA prefixed error codes are used instead. Windows Sockets functions return SOCKET_ERROR on error, BSD sockets code commonly check for the value 1 to test for errors. Note that under Windows, using C++, Microsoft Foundation Classes (MFC) provides a wrapper class (Csocket). I shall leave the reader to investigate this, and concentrate on teh C API since this is more universal. COPYRIGHT CLIFFORD SLOCOMBE PAGE 3 OF 8

4 Byte order and structure packing Care must be taken when transferring data between two machines over byte ordering and structure packing, which may differ between different CPU and/or compilers. This means, for example that a multi-byte integer, or a data structure transmitted as a byte stream from one computer, may not be directly read into the same data type on another. To help overcome some of these problems a convention known as network byte order is used. A number of macros are used to convert between the local host byte order and network byte order. The macros impose no overhead on architectures wherethe host byte order matches network byte order. The macros defined in <in.h> are as follows: Macro htonl htons ntohl ntohs Description Convert a long from host to network byte ordering. Convert a short from host to network byte ordering. Convert a long from network to host byte ordering. Convert a short from network to host byte ordering. Differences in byte ordering between the CPU and the network convention may or may not be important for application data. It is however important with respect to network addressing, where the use of the macros is essential to ensure correct addressing and portability of code. Establishing a communication session A connection is initiated an application running on one node on the network, and accepted by another application running on another node (or possibly the same node). Often a client-server relationship is established, where the client requests a connection to the server, and the server can accept one or more simultaneous connections. It is also possible to create peer-peer applications where bothe ends of a connection may either initiate or accept connections. The example considered here is of the client-server form. It is also very simple in that the server may only accept a connection from and service one client at a time. To service multiple clients (a concurrent server), the server application would have to accept a connection and then spawn a separate thread to service the client, before returning to 'listen' for other clients. Since creating multiple threads or tasks is operating system dependent, I have not dealt with it in detail here. Client The steps taken by a client application during a communication session are: Obtain a socket descriptor (handle) by calling the socket() function. Initialise a sockaddr_in structure with addressing data. Connect to the server by calling the connect() function. Send and receive data as necessary by calling the send() and recv() functions as necessary. Alternatively, under BSD socket API the file IO functions read() and write() may be used. Terminate connection by calling close() (BSD) or closesocket() (WinSock). Server The steps taken by a client application during a communication session are: Obtain a server socket descriptor (handle) by calling the socket() function. Initialise a sockaddr_in structure with addressing data. Bind the addressing information to the socket by calling the bind() function. Wait for connection requests on the selected port by calling listen(). COPYRIGHT CLIFFORD SLOCOMBE PAGE 4 OF 8

5 Accept the connection (and get the client socket descriptor) by calling accept(). If the application is a concurrent server, a new thread world be spawned (or an existing thread awoken) to accept the connection before looping back to listen. The new thread would communicate with the client using socket descriptor returned by accept(). Send and receive data as necessary by calling the send() and recv() functions as necessary. Alternatively, under BSD socket API the file IO functions read() and write() may be used. Terminate the client connection by calling close() (BSD) or closesocket() (WinSock) for the client descriptor. When the server terminates, close the server socket by calling close() (BSD) or closesocket() (WinSock) for the client descriptor. A simple example application The application described herein implements a simple TCP/IP application called rprint. It is intended to demonstrate the basics of communicating data via TCP/IP with the minimum of superfluous code. The simplicity is intended to enable the reader to appreciate the essential requirements of TCP/IP applications, without unnecessarily elaborate application code. The rprint application is a client/server application in two parts, a client application, and a server application. For simplicity (and portability), the applications are console (text) mode applications, allowing us to ignorethe added complexity and rigors of GUI applications. The client program is called rprint, it takes an IP address and a port number, and then any number of arbitrary strings as parameters, for example: rprint Hello, world The strings are sent with spaces delimiting them and a NUL terminator to the port at the IP address. The server end is called rprintsvr, and simply prints any NUL terminated string it receives followed by a newline character. The application is very simple, data flows in one direction only, and the server can service exactly one client at a time. The code has been verbosely commented since it is intended to be instructive. It also contains conditional compilation so that it should build on either WinSock or BSD environments. If you intend to develop on only one environment, you could render the code more readable by removing the parts related to the alternate environment. However, writing the code for both platforms in a single source does serve to highlight the differences. Client code for BSD/WinSock / rprint console mode socket client Clifford Slocombe - December 2002 A simple demonstration of a socket client application Sends null terminated string of words passed on the command line to remote server for display. USAGE: rprint <ipaddr> <port> <word> [<word>...] e.g. rprint This is an rprint message. / #include <winsock.h> / Windows Sockets header / #include <socket.h> / BSD sockets library / #include <io.h> / BSD sockets use file I/O / #include <in.h> / network byte order macros / / These error macros are used in Winsock, and are defined here so that the BSD code can be the same. Avoiding lots (more) of messy conditional compilation / #if!defined SOCKET_ERROR #define SOCKET_ERROR -1 #if!defined INVALID_SOCKET #define INVALID_SOCKET -1 #include <string.h> COPYRIGHT CLIFFORD SLOCOMBE PAGE 5 OF 8

6 int main( int argc, charargv ) / Console mode, so main() is OK / WSADATA winsock_info ; / Required by WASStartup, gets filled with information about WinSock implementation / SOCKET sd ; / Socket descriptor (not a file handle in Windows) / SOCKADDR_IN name ; / IP addressing data the term name is a BSD convention / int sd ; / Socket descriptor (file handle in BSD) / struct sockaddr_in name ; / IP addressing data the term name is a BSD convention / int status ; int param ; / WSAStartup is mandatory in WinSock applications / status = WSAStartup( MAKEWORD(1,1), &winsock_info ) ; / No Startup required in BSD, set status to zero so subsequent test suceeds / status = 0 ; if( status == 0 ) / Get a socket descriptor / sd = socket( PF_INET, SOCK_STREAM, 0 ) ; / Test for valid socket... / if( sd!= INVALID_SOCKET ) / Got a good socket, so fill in addressing data / name.sin_family = AF_INET ; / Internet addressing / name.sin_addr.s_addr = inet_addr( argv[1] ) ; / IP address (convert from a.b.c.d string) / name.sin_port = htons( (u_short)atoi(argv[2]) ) ; / Port, (network byte order) / / If IP a.b.c.d address string was valid / if(name.sin_addr.s_addr!= INADDR_NONE ) / Connect to server, (cast address to generic socket address) / status = connect( sd, (struct sockaddr)&name, sizeof(name) ) ; else / Ensure loop will not start if error / status = SOCKET_ERROR ; / For each remaining parameter, or until error... / for( param = 3; status!= SOCKET_ERROR && param < argc; param++ ) / Send parameter string / status = send( sd, argv[param], strlen(argv[param]), 0 ) ; / If no error... / if( status!= SOCKET_ERROR ) / Send a space character / status = send( sd, " ", 1, 0 ) ; / Terminate transmission with a NUL character / (void)send( sd, "\0", 1, 0 ) ; / Close socket with WinSock specific function, BSD close() not guaranteed because descriptor is not a file handle / closesocket( sd ) ; close( sd ) ; / WSACleanup is mandatory in WinSock applications / WSACleanup() ; return( 0 ) ; COPYRIGHT CLIFFORD SLOCOMBE PAGE 6 OF 8

7 Server code for BSD/WinSock / rprintsvr console mode socket server Clifford Slocombe - December 2002 A simple demonstration of a BSD Sockets server application Recieves NUL terminated strings abd displays them. USAGE: rprintsvr port Port parameter must match that used by client / #include <string.h> #include <stdlib.h> #include <stdio.h> #include <winsock.h> / Windows Sockets header / #include <socket.h> / BSD sockets library / #include <io.h> / BSD sockets use file I/O / #include <in.h> / network byte order macros / / These error macros are used in Winsock, and are defined here so that the BSD code can be the same. Avoiding lots (more) of messy conditional compilation / #if!defined SOCKET_ERROR #define SOCKET_ERROR -1 #if!defined INVALID_SOCKET #define INVALID_SOCKET -1 int main( int argc, charargv ) / Console mode, so main() is OK / unsigned int port ; SOCKET server_sd ; / Server socket descriptor / SOCKET client_sd ; / Client socket descriptor / SOCKADDR_IN server_name ; / Server IP addressing / SOCKADDR_IN client_name ; / Client IP addressing / int server_sd ; / Server socket descriptor / int client_sd ; / Client socket descriptor / struct sockaddr_in server_name ; / Server IP addressing / struct sockaddr_in client_name ; / Client IP addressing / int status = SOCKET_ERROR ; int addr_len = sizeof(client_name) ; char c ; / Get port address from command line / port = atoi( argv[1] ) ; / get a server side socket / server_sd = socket( PF_INET, SOCK_STREAM, 0 ) ; / accept() requires pointer to address length so address length must be assigned to a variable / / In BSD valid sockets are positive integers / if( server_sd!= INVALID_SOCKET ) / Got a good socket, so fill in addressing data / memset( &server_name, sizeof(server_name), 0 ) ; server_name.sin_family = AF_INET ; / Internet addressing / server_name.sin_addr.s_addr = INADDR_ANY ; / accept input from any client / server_name.sin_port = htons( port ) ; / but just at this port / / Bind addressing to server socket / status = bind( server_sd, (struct sockaddr)&server_name, sizeof(struct sockaddr) ) ; if( status!= ERROR ) / Wait for activity on bound port, in this case only allowing one pending connection, because this is not a concurrent server / status = listen( server_sd, 1 ) ; while( status!= ERROR ) / accept the connection, binding it to the client descriptor / client_sd = accept( server_sd, (struct sockaddr)&client_name, &addr_len ) ; if( client_sd >= 0 ) / Recieve characters, and display them until a NUL is read / COPYRIGHT CLIFFORD SLOCOMBE PAGE 7 OF 8

8 status = recv( client_sd, &c, 1, 0 ) ; while( c!= '\0' && status!= SOCKET_ERROR ) putchar( (int)c ) ; / display / status = recv( client_sd, &c, 1, 0 ) ; / get next / putchar( '\n' ) ; / Close client socket when done / / Close socket with WinSock specific function, BSD close() not guaranteed because descriptor is not a file handle / closesocket( client_sd ) ; close( client_sd ) ; else status = ERROR ; / Close server socket if it was ever opened / if( server_sd!= SOCKET_ERROR ) / Close socket with WinSock specific function, BSD close() not guaranteed because descriptor is not a file handle / closesocket( server_sd ) ; close( server_sd ) ; return( 0 ) ; Practical applications The example in the section illustrates the essentials of network communication using sockets. In a practical application, it would be likely that multiple clients could be serviced simultaneously, and that data would be exchanged in both directions. Typical server applications incorporate a loop, where the accept is performed. For each accepted connection, a task is either spawned or awoken, the client socket descriptor is passed to the task, so that that client can be serviced, while the server loop is ready to accept new connections. COPYRIGHT CLIFFORD SLOCOMBE PAGE 8 OF 8

CSCE 463/612 Networks and Distributed Processing Spring 2017

CSCE 463/612 Networks and Distributed Processing Spring 2017 CSCE 463/612 Networks and Distributed Processing Spring 2017 Preliminaries II Dmitri Loguinov Texas A&M University January 19, 2017 1 Agenda HTTP basics Windows sockets Clients 2 HTTP Basics General URL

More information

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

WinSock. What Is Sockets What Is Windows Sockets What Are Its Benefits Architecture of Windows Sockets Network Application Mechanics WinSock What Is Sockets What Is Windows Sockets What Are Its Benefits Architecture of Windows Sockets Network Application Mechanics What Is Sockets Standard API (Application Programming Interface) for

More information

Socket Programming for TCP and UDP

Socket Programming for TCP and UDP CSCI4430 Data Communication and Computer Networks Socket Programming for TCP and UDP ZHANG, Mi Jan. 19, 2017 Outline Socket Programming for TCP Introduction What is TCP What is socket TCP socket programming

More information

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

Hybrid of client-server and P2P. Pure P2P Architecture. App-layer Protocols. Communicating Processes. Transport Service Requirements Announcements CS 5565 Network Architecture and Protocols Lecture 5 Godmar Back Problem Set 1 due Feb 17 Project 1 handed out shortly 2 Layer The Layer Let s look at some s (in keeping with top-down) architectures:

More information

CS321: Computer Networks Introduction to Application Layer

CS321: Computer Networks Introduction to Application Layer CS321: Computer Networks Introduction to Application Layer Dr. Manas Khatua Assistant Professor Dept. of CSE IIT Jodhpur E-mail: manaskhatua@iitj.ac.in Basic Application layer provides services to the

More information

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

Lecture 24. Thursday, November 19 CS 375 UNIX System Programming - Lecture 24 1 Lecture 24 Log into Linux. Copy directory /home/hwang/cs375/lecture24 Final project posted. Due during finals week. Reminder: No class next Tuesday (11/24) Questions? Thursday, November 19 CS 375 UNIX

More information

Socket Programming. Sungkyunkwan University. Hyunseung Choo Copyright Networking Laboratory

Socket Programming. Sungkyunkwan University. Hyunseung Choo Copyright Networking Laboratory Socket Programming Sungkyunkwan University Hyunseung Choo choo@skku.edu Copyright 2000-2019 Networking Laboratory Contents Goals Client-Server mechanism Introduction to socket Programming with socket on

More information

A Client-Server Exchange

A Client-Server Exchange Socket programming A Client-Server Exchange A server process and one or more client processes Server manages some resource. Server provides service by manipulating resource for clients. 1. Client sends

More information

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

Ports under 1024 are often considered special, and usually require special OS privileges to use. 1 2 Turns out that besides an IP address (used by the IP layer), there is another address that is used by TCP (stream sockets) and, coincidentally, by UDP (datagram sockets). It is the port number. It's

More information

2007 Microsoft Corporation. All rights reserved.

2007 Microsoft Corporation. All rights reserved. Creating a Basic Winsock Application 2007 Microsoft Corporation. All rights reserved. To create a basic Winsock application 1. Create a new empty project. 2. Add an empty C++ source file to the project.

More information

CS321: Computer Networks Socket Programming

CS321: Computer Networks Socket Programming CS321: Computer Networks Socket Programming Dr. Manas Khatua Assistant Professor Dept. of CSE IIT Jodhpur E-mail: manaskhatua@iitj.ac.in Socket Programming It shows how the network application programs

More information

Linux Network Programming, Part 1

Linux Network Programming, Part 1 Linux Network Programming, Part 1 http://www.linuxjournal.com/article/2333?page=0,0 Feb 01, 1998 By Ivan Griffin and John Nelson This is the first of a series of articles about how to develop networked

More information

ECE 435 Network Engineering Lecture 2

ECE 435 Network Engineering Lecture 2 ECE 435 Network Engineering Lecture 2 Vince Weaver http://web.eece.maine.edu/~vweaver vincent.weaver@maine.edu 6 September 2018 Announcements Homework 1 will be posted. Will be on website, will announce

More information

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

Oral. Total. Dated Sign (2) (5) (3) (2) R N Oral Total Dated Sign (2) (5) (3) (2) Assignment Group- A_07 Problem Definition Write a program using TCP socket for wired network for following Say Hello to Each other ( For all students) File transfer

More information

A. Basic Function Calls for Network Communications

A. Basic Function Calls for Network Communications IV. Network Programming A. Basic Function Calls for Network Communications 1 B. Settings for Windows Platform (1) Visual C++ 2008 Express Edition (free version) 2 (2) Winsock Header and Libraries Include

More information

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

Computer Network Lab, SS Fachgebiet Technische Informatik, Joachim Zumbrägel. Overview. Sockets. Sockets in C. Computer Network Lab 2016 Fachgebiet Technische Informatik, Joachim Zumbrägel Overview Sockets Sockets in C Sockets in Delphi 1 Inter process communication There are two possibilities when two processes

More information

Hyo-bong Son Computer Systems Laboratory Sungkyunkwan University

Hyo-bong Son Computer Systems Laboratory Sungkyunkwan University Sockets Hyo-bong Son (proshb@csl.skku.edu) Computer Systems Laboratory Sungkyunkwan University http://csl.skku.edu Client-Server Model Most network application is based on the client-server model: A server

More information

Client Server Computing

Client Server Computing Client Server Computing Although the Internet provides a basic communication service, the protocol software cannot initiate contact with, or accept contact from, a remote computer. Instead, two application

More information

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

Context. Distributed Systems: Sockets Programming. Alberto Bosio, Associate Professor UM Microelectronic Departement Distributed Systems: Sockets Programming Alberto Bosio, Associate Professor UM Microelectronic Departement bosio@lirmm.fr Context Computer Network hosts, routers, communication channels Hosts run applications

More information

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

The BSD UNIX Socket Interface (CS 640 Lecture) Assignment 1. Interprocess Communication (IPC) Work Individually (no groups) The BSD UNIX Socket Interface (CS 640 Lecture) Assignment 1 Work Individually (no groups) Due Date: in class, Monday, September 19 Robert T Olsen olsen@cswiscedu 7390CS Office Hours: 3-5T, 11-12F - exception

More information

Introduction to Computer Networks

Introduction to Computer Networks Introduction to Computer Networks Tian Song ( 嵩天 ), Ph.D., Assoc. Prof. songtian@bit.edu.cn Introduction to Computer Networks Socket and Network Programming Tian Song ( 嵩天 ), Ph.D., Assoc. Prof. songtian@bit.edu.cn

More information

What s an API? Do we need standardization?

What s an API? Do we need standardization? Network Interface z The network protocol stack is a part of the OS z Need an API to interface applications to the protocol stack. What s an API? Do we need standardization? z The socket interface is the

More information

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

Socket Programming. CSIS0234A Computer and Communication Networks. Socket Programming in C 1 CSIS0234A Computer and Communication Networks Socket Programming in C References Beej's Guide to Network Programming Official homepage: http://beej.us/guide/bgnet/ Local mirror http://www.cs.hku.hk/~c0234a/bgnet/

More information

Tutorial on Socket Programming

Tutorial on Socket Programming Tutorial on Socket Programming Computer Networks - CSC 458 Department of Computer Science Hao Wang (Slides are mainly from Seyed Hossein Mortazavi, Monia Ghobadi, and Amin Tootoonchian, ) 1 Outline Client-server

More information

31 ChamSys Remote Protocol Commands

31 ChamSys Remote Protocol Commands 31 ChamSys Remote Protocol Commands ChamSys remote protocol consists of simple commands consisting of a list parameter values separated by commas, and ending in a character A to Z (or a to z). Commands

More information

How to write a Measurement Telnet Server

How to write a Measurement Telnet Server How to write a Measurement Telnet Server A measurement Telnet server allows you to access remote I/Os with a standard Telnet client program. The following samples shows a way to set the LEDs of a DNP/EVA1

More information

EECS122 Communications Networks Socket Programming. Jörn Altmann

EECS122 Communications Networks Socket Programming. Jörn Altmann EECS122 Communications Networks Socket Programming Jörn Altmann Questions that will be Addressed During the Lecture What mechanisms are available for a programmer who writes network applications? How to

More information

Unix Network Programming

Unix Network Programming Introduction to Computer Networks Polly Huang EE NTU Unix Network Programming The socket struct and data handling System calls Based on Beej's Guide to Network Programming 1 The Unix Socket A file descriptor

More information

ECE 435 Network Engineering Lecture 2

ECE 435 Network Engineering Lecture 2 ECE 435 Network Engineering Lecture 2 Vince Weaver http://web.eece.maine.edu/~vweaver vincent.weaver@maine.edu 31 August 2017 Announcements Homework 1 will be posted. Will be on website, will announce

More information

Windows Sockets: A Quick And Dirty Primer

Windows Sockets: A Quick And Dirty Primer Windows Sockets: A Quick And Dirty Primer Page 1 of 11 Windows Sockets: A Quick And Dirty Primer by Jim Frost Last modified December 31, 1999 Contents Introduction What is a socket, anyway? (or: The Analogy)

More information

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

NETWORK PROGRAMMING. Instructor: Junaid Tariq, Lecturer, Department of Computer Science NETWORK PROGRAMMING CSC- 341 25 Instructor: Junaid Tariq, Lecturer, Department of Computer Science 26 9 Lecture Sockets as means for inter-process communication (IPC) application layer Client Process Socket

More information

CS 3516: Computer Networks

CS 3516: Computer Networks Welcome to CS 3516: Prof. Yanhua Li Time: 9:00am 9:50am M, T, R, and F Location: AK219 Fall 2018 A-term 1 Some slides are originally from the course materials of the textbook Computer Networking: A Top

More information

Socket Programming TCP UDP

Socket Programming TCP UDP Socket Programming TCP UDP Introduction Computer Network hosts, routers, communication channels Hosts run applications Routers forward information Packets: sequence of bytes contain control information

More information

CS 640: Computer Networking

CS 640: Computer Networking CS 640: Computer Networking Yu-Chi Lai Lecture 3 Network Programming Topics Client-server model Sockets interface Socket primitives Example code for echoclient and echoserver Debugging With GDB Programming

More information

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

Socket Programming. Dr. -Ing. Abdalkarim Awad. Informatik 7 Rechnernetze und Kommunikationssysteme Socket Programming Dr. -Ing. Abdalkarim Awad Informatik 7 Rechnernetze und Kommunikationssysteme Before we start Can you find the ip address of an interface? Can you find the mac address of an interface?

More information

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

Sockets. Dong-kun Shin Embedded Software Laboratory Sungkyunkwan University  Embedded Software Lab. 1 Sockets Dong-kun Shin Embedded Software Laboratory Sungkyunkwan University http://nyx.skku.ac.kr Internet Connections (1) 2 Connection Clients and servers communicate by sending streams of bytes over

More information

Elementary TCP Sockets

Elementary TCP Sockets Elementary TCP Sockets Chapter 4 UNIX Network Programming Vol. 1, Second Ed. Stevens Distributed Computer Systems 1 socket interface Application 1 Application 2 socket interface user kernel user kernel

More information

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

TCP/IP Sockets in C: Practical Guide for Programmers. Computer Chat. Internet Protocol (IP) IP Address. Transport Protocols. Ports TCP/IP Sockets in C: Practical Guide for Programmers Computer Chat! How do we make computers talk? Michael J. Donahoo Kenneth L. Calvert Morgan Kaufmann Publisher $14.95 Paperback! How are they interconnected?

More information

Socket Programming 2007/03/28

Socket Programming 2007/03/28 Socket Programming 2007/03/28 Reference W. Richard Stevens, Unix Network Programming 2/e Volume 1,1998 James F. Kurose and Keith W. Ross, "Computer Networks: A Top-Down Approach Featuring the Internet

More information

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

Lecture 7. Followup. Review. Communication Interface. Socket Communication. Client-Server Model. Socket Programming January 28, 2005 Followup symbolic link (soft link): pathname, can be across file systems, replacement of file will be active on all symbolic links, consumes at least an inode. hard link: pointers to an inode, only in

More information

SOCKETS. COMP750 Distributed Systems

SOCKETS. COMP750 Distributed Systems SOCKETS COMP750 Distributed Systems Sockets The Socket library is a traditional Application Program Interface (API) to the transport layer. Sockets were originally implemented in Unix systems and have

More information

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

Network Programming in C: The Berkeley Sockets API. Networked Systems 3 Laboratory Sessions Network Programming in C: The Berkeley Sockets API Networked Systems 3 Laboratory Sessions The Berkeley Sockets API Widely used low-level C networking API First introduced in 4.3BSD Unix Now available

More information

Winsock Server adding Multiple clients support C++

Winsock Server adding Multiple clients support C++ Winsock Server adding Multiple clients support C++ This is very simple version to add multiple connection support to your server project. Here we are going to use ioctlsocket to make it non blocking and

More information

Network Software Implementations

Network Software Implementations Network Software Implementations Number of computers on the Internet doubling yearly since 1981, nearing 200 million Estimated that more than 600 million people use the Internet Number of bits transmitted

More information

Session NM056. Programming TCP/IP with Sockets. Geoff Bryant Process software

Session NM056. Programming TCP/IP with Sockets. Geoff Bryant Process software Session NM056 Programming TCP/IP with Sockets Geoff Bryant Process software Course Roadmap Slide 57 NM055 (11:00-12:00) Important Terms and Concepts TCP/IP and Client/Server Model Sockets and TLI Client/Server

More information

ELEC / COMP 177 Fall Some slides from Kurose and Ross, Computer Networking, 5 th Edition

ELEC / COMP 177 Fall Some slides from Kurose and Ross, Computer Networking, 5 th Edition ELEC / COMP 177 Fall 2014 Some slides from Kurose and Ross, Computer Networking, 5 th Edition Project #1 Starts in one week Is your Linux environment all ready? Bring your laptop Work time after quick

More information

UNIX Network Programming. Overview of Socket API Network Programming Basics

UNIX Network Programming. Overview of Socket API Network Programming Basics UNIX Network Programming Overview of Socket API Network Programming Basics 1 Client-Server Model Client Machine A Network Server Machine B Web browser and server FTP client and server Telnet client and

More information

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

Networked Applications: Sockets. End System: Computer on the Net Networked Applications: Sockets Topics Programmer s view of the Internet Sockets interface End System: Computer on the Net Internet Also known as a host 2 Page 1 Clients and Servers Client program Running

More information

EECS 123: Introduction to Real-Time Distributed Programming

EECS 123: Introduction to Real-Time Distributed Programming EECS 123: Introduction to Real-Time Distributed Programming Lecture : IP, UDP, TCP, RPC, and Time Measurement This slide-set was prepared by the Teaching Assistant, P. Athreya. * Most of the slides in

More information

Sockets. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

Sockets. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University Sockets Jin-Soo Kim (jinsookim@skku.edu) Computer Systems Laboratory Sungkyunkwan University http://csl.skku.edu Internet Connections (1) Connection Clients and servers communicate by sending streams of

More information

Programming with TCP/IP. Ram Dantu

Programming with TCP/IP. Ram Dantu 1 Programming with TCP/IP Ram Dantu 2 Client Server Computing Although the Internet provides a basic communication service, the protocol software cannot initiate contact with, or accept contact from, a

More information

Network Socket Programming - 3 BUPT/QMUL

Network Socket Programming - 3 BUPT/QMUL Network Socket Programming - 3 BUPT/QMUL 2017-3-27 Agenda Basic concepts in NP Introduction to IP & TCP/UDP Introduction to Sockets 2 Introduction to Sockets Reviews of some helpful points Sockets interface

More information

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

Network Programming Worksheet 2. Simple TCP Clients and Servers on *nix with C. Simple TCP Clients and Servers on *nix with C. Aims. This worksheet introduces a simple client and a simple server to experiment with a daytime service. It shows how telnet can be used to test the server.

More information

Lab 0. Yvan Petillot. Networks - Lab 0 1

Lab 0. Yvan Petillot. Networks - Lab 0 1 Lab 0 Yvan Petillot Networks - Lab 0 1 What You Will Do In This Lab. The purpose of this lab is to help you become familiar with the UNIX/LINUX on the lab network. This means being able to do editing,

More information

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

Socket Programming. #In the name of Allah. Computer Engineering Department Sharif University of Technology CE443- Computer Networks #In the name of Allah Computer Engineering Department Sharif University of Technology CE443- Computer Networks Socket Programming Acknowledgments: Lecture slides are from Computer networks course thought

More information

Announcements. CS 5565 Network Architecture and Protocols. Queuing. Demultiplexing. Demultiplexing Issues (1) Demultiplexing Issues (2)

Announcements. CS 5565 Network Architecture and Protocols. Queuing. Demultiplexing. Demultiplexing Issues (1) Demultiplexing Issues (2) Announcements CS 5565 Network Architecture and Protocols Problem Set 1 due Feb 18 Project 1A due Feb 19 Lecture 5 Godmar Back 2 Queuing Demultiplexing send queues Layer k+1 Layer k recv queues End systems

More information

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

Processes communicating. Network Communication. Sockets. Addressing processes 4/15/2013 Processes communicating Network Communication Process: program running within a host. within same host, two processes communicate using inter-process communication (defined by OS). processes in different

More information

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

Christian Tschudin (basierend auf einem Foliensatz von C. Jelger und T. Meyer) Departement Mathematik und Informatik, Universität Basel Internettechnologien (CS262) Socket Programming in C 4. März 2015 Christian Tschudin (basierend auf einem Foliensatz von C. Jelger und T. Meyer) Departement Mathematik und Informatik, Universität Basel

More information

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

Lecture 2. Outline. Layering and Protocols. Network Architecture. Layering and Protocols. Layering and Protocols. Chapter 1 - Foundation Lecture 2 Outline Wireshark Project 1 posted, due in a week Lab from a different textbook Work through the lab and answer questions at the end Chapter 1 - Foundation 1.1 Applications 1.2 Requirements 1.3

More information

COMP/ELEC 429/556 Introduction to Computer Networks

COMP/ELEC 429/556 Introduction to Computer Networks COMP/ELEC 429/556 Introduction to Computer Networks Creating a Network Application Some slides used with permissions from Edward W. Knightly, T. S. Eugene Ng, Ion Stoica, Hui Zhang 1 How to Programmatically

More information

How to write a Measurement Telnet Server

How to write a Measurement Telnet Server How to write a Measurement Telnet Server A measurement Telnet server allows you to access remote I/Os with a standard Telnet client program. The following samples shows a way to set the LEDs of a DNP/EVA2

More information

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

Network Programming in C. Networked Systems 3 Laboratory Sessions and Problem Sets Network Programming in C Networked Systems 3 Laboratory Sessions and Problem Sets Lab Timetable, Aims, and Objectives Teaching Week Activity 14 Introduction 15 Warm-up exercise 16 17 Web client 18 19 20

More information

Network Socket Programming - 3 BUPT/QMUL

Network Socket Programming - 3 BUPT/QMUL Network Socket Programming - 3 BUPT/QMUL 2018-04-02 Agenda Basic concepts in NP Introduction to IP & TCP/UDP Introduction to Sockets 2 Introduction to Sockets Reviews of some helpful points Sockets interface

More information

Network programming(i) Lenuta Alboaie

Network programming(i) Lenuta Alboaie Network programming(i) Lenuta Alboaie adria@info.uaic.ro 2017 2018 Computer Network http://www.info.uaic.ro/~computernetworks 1 Content Client/server paradigm API for network programming BSD Socket Characteristics

More information

Communication. Communication. Distributed Systems. Networks and protocols Sockets Remote Invocation Messages Streams. Fall /10/2001 DoCS

Communication. Communication. Distributed Systems. Networks and protocols Sockets Remote Invocation Messages Streams. Fall /10/2001 DoCS Communication Distributed Systems Fall 2002 Communication Process Process Networks and protocols Sockets Remote Invocation Messages Streams 9/10/2001 DoCS 2002 2 Layered Protocols (1) Layers, interfaces,

More information

MSc Integrated Electronics Networks Assignment. Investigation of TCP/IP Sockets and Ports. Gavin Cameron

MSc Integrated Electronics Networks Assignment. Investigation of TCP/IP Sockets and Ports. Gavin Cameron MSc Integrated Electronics Networks Assignment Investigation of TCP/IP Sockets and Ports Gavin Cameron Introduction TCP and IP (Transmission Control Protocol / Internet Protocol) are two protocols from

More information

The Berkeley Sockets API. Networked Systems Architecture 3 Lecture 4

The Berkeley Sockets API. Networked Systems Architecture 3 Lecture 4 The Berkeley Sockets API Networked Systems Architecture 3 Lecture 4 The Berkeley Sockets API Widely used low-level C networking API First introduced in 4.3BSD Unix Now available on most platforms: Linux,

More information

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

CS118 Discussion 1B, Week 1. Taqi Raza BUNCHE 1209B, Fridays 12:00pm to 1:50pm CS118 Discussion 1B, Week 1 Taqi Raza BUNCHE 1209B, Fridays 12:00pm to 1:50pm 1 TA Taqi, PhD student in Computer Networking Discussion (1B): Bunche 1209, Fri 12:00 1:50 p.m. Office hours: Boelter Hall

More information

Group-A Assignment No. 6

Group-A Assignment No. 6 Group-A Assignment No. 6 R N Oral Total Dated Sign (2) (5) (3) (10) Title : File Transfer using TCP Socket Problem Definition: Use Python for Socket Programming to connect two or more PCs to share a text

More information

CSCI 415 Computer Networks Homework 2 Due 02/13/08

CSCI 415 Computer Networks Homework 2 Due 02/13/08 CSCI 415 Computer Networks Homework 2 Due 02/13/08 Saad Mneimneh Computer Science Hunter College of CUNY Problem 1 Consider the following server and client C++ code that we saw in class: server.c #include

More information

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

PA #2 Reviews. set_name, get_name, del_name. Questions? Will be modified after PA #4 ~ Sockets Prof. Jin-Soo Kim( jinsookim@skku.edu) TA Dong-Yun Lee(dylee@csl.skku.edu) Computer Systems Laboratory Sungkyunkwan University http://csl.skku.edu PA #2 Reviews set_name, get_name, del_name Will

More information

JFx Joystick. June 21, Unrestricted Distribution. Revision 1.00

JFx Joystick. June 21, Unrestricted Distribution. Revision 1.00 JFx Joystick TCP Software Interface Revision 1.00 June 21, 2011 Unrestricted Distribution Table of Contents 1 Document Purpose 2 1.1 Conventions 2 2 Software 3 2.1 Startup 3 2.1.1 Initialization 3 2.1.2

More information

CS307 Operating Systems Processes

CS307 Operating Systems Processes CS307 Processes Fan Wu Department of Computer Science and Engineering Shanghai Jiao Tong University Spring 2018 Process Concept Process a program in execution An operating system executes a variety of

More information

Processes. Process Concept. The Process. The Process (Cont.) Process Control Block (PCB) Process State

Processes. Process Concept. The Process. The Process (Cont.) Process Control Block (PCB) Process State CS307 Process Concept Process a program in execution Processes An operating system executes a variety of programs: Batch system jobs Time-shared systems user programs or tasks All these activities are

More information

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

Types (Protocols) Associated functions Styles We will look at using sockets in C Java sockets are conceptually quite similar Socket Programming What is a socket? Using sockets Types (Protocols) Associated functions Styles We will look at using sockets in C Java sockets are conceptually quite similar - Advanced Data Communications:

More information

CSC209H Lecture 9. Dan Zingaro. March 11, 2015

CSC209H Lecture 9. Dan Zingaro. March 11, 2015 CSC209H Lecture 9 Dan Zingaro March 11, 2015 Socket Programming (Kerrisk Ch 56, 57, 59) Pipes and signals are only useful for processes communicating on the same machine Sockets are a general interprocess

More information

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

UNIX Sockets. Developed for the Azera Group By: Joseph D. Fournier B.Sc.E.E., M.Sc.E.E. UNIX Sockets Developed for the Azera Group By: Joseph D. Fournier B.Sc.E.E., M.Sc.E.E. Socket and Process Communication application layer User Process Socket transport layer (TCP/UDP) network layer (IP)

More information

Introduction to Client-Server Model

Introduction to Client-Server Model Preview Introduction to Client-Server Model Motivation of Client-Server Model Terminologies and Concepts in Client-Server Model Connectionless vs. Connection-Oriented Stateless vs. Stateful Server Identify

More information

UNIX Sockets. COS 461 Precept 1

UNIX Sockets. COS 461 Precept 1 UNIX Sockets COS 461 Precept 1 Socket and Process Communica;on application layer User Process Socket transport layer (TCP/UDP) OS network stack network layer (IP) link layer (e.g. ethernet) Internet Internet

More information

Network Communication

Network Communication Network Communication Processes communicating Process: program running within a host. q within same host, two processes communicate using inter- process communica6on (defined by OS). q processes in different

More information

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

SOCKET PROGRAMMING. What is a socket? Using sockets Types (Protocols) Associated functions Styles LABORATORY SOCKET PROGRAMMING What is a socket? Using sockets Types (Protocols) Associated functions Styles 2 WHAT IS A SOCKET? An interface between application and network The application creates a socket

More information

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

Networked Applications: Sockets. Goals of Todayʼs Lecture. End System: Computer on the ʻNet. Client-server paradigm End systems Clients and servers Networked Applications: Sockets CS 375: Computer Networks Spring 2009 Thomas Bressoud 1 Goals of Todayʼs Lecture Client-server paradigm End systems Clients and servers Sockets and Network Programming Socket

More information

TCP Network Programming in C

TCP Network Programming in C CPSC 360 Network Programming TCP Network Programming in C Michele Weigle Department of Computer Science Clemson University mweigle@cs.clemson.edu http://www.cs.clemson.edu/~mweigle/courses/cpsc360 1 Sockets

More information

System Programming. Sockets: examples

System Programming. Sockets: examples Content : by Dr. B. Boufama School of Computer Science University of Windsor Instructor: Dr. A. Habed adlane@cs.uwindsor.ca http://cs.uwindsor.ca/ adlane/60-256 Content Content 1 Socket based client/server

More information

ICT 6544 Distributed Systems Lecture 5

ICT 6544 Distributed Systems Lecture 5 ICT 6544 Distributed Systems Lecture 5 Hossen Asiful Mustafa Message Brokers Figure 4-21. The general organization of a message broker in a message-queuing system. IBM s WebSphere Message-Queuing System

More information

Sockets 15H2. Inshik Song

Sockets 15H2. Inshik Song Sockets 15H2 Inshik Song Internet CAU www server (www.cau.ac.kr) Your web browser (Internet Explorer/Safari) Sockets 2 How do we find the server? Every computer on the Internet has an Internet address.

More information

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

How do we Communicate? Introduction to Unix Network Programming. What does Alice do? What does Bob do? Two simplest networking programs Introduction to Unix Network Programming Reference: Stevens Unix Network Programming How do we Communicate? Send a mail from Alice to Bob Bob Alice in Champaign, Bob in Hollywood Example: US Postal Service

More information

Assignment description: This is a C++ project. The comms class containing the

Assignment description: This is a C++ project. The comms class containing the Assignment description: This is a C++ project. The comms class containing the code that is common to both the client and server. The classes should contain the functionality required to establishing a

More information

Assignment 1: tcpbridge

Assignment 1: tcpbridge Assignment 1: tcpbridge 1 The assignment is to create a program that forwards the data from the bridge port (a) to other host's port (b). The target host's address details and port numbers to be used are

More information

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

A Socket Example. Haris Andrianakis & Angelos Stavrou George Mason University A Socket Example & George Mason University Everything is a file descriptor Most socket system calls operate on file descriptors Server - Quick view socket() bind() listen() accept() send(), recv() close()

More information

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

Project 3. Reliable Data Transfer over UDP. NTU CSIE Computer Networks 2011 Spring Project 3 Reliable Data Transfer over UDP NTU CSIE Computer Networks 2011 Spring Project Goal In Project 3, students are asked to understand and implement reliable data transfer mechanism over UDP. UDP

More information

// socket for establishing connections

// socket for establishing connections #include #include #include #include #include #include #include #define FILELENGTH 511 // This is not right #define

More information

Introduction and Overview Socket Programming Higher-level interfaces Final thoughts. Network Programming. Samuli Sorvakko/Nixu Oy

Introduction and Overview Socket Programming Higher-level interfaces Final thoughts. Network Programming. Samuli Sorvakko/Nixu Oy Network Programming Samuli Sorvakko/Nixu Oy Telecommunications software and Multimedia Laboratory T-110.4100 Computer Networks October 9, 2006 Agenda 1 Introduction and Overview Introduction 2 Socket Programming

More information

Network Programming Week #1. K.C. Kim

Network Programming Week #1. K.C. Kim Network Programming Week #1 K.C. Kim kckim@konkuk.ac.kr How do we communicate? Mail Example 1. Write a mail 2. Put the mail into a mailbox 3. Post office classify mails based on the address 4. Cars, airplanes

More information

CSE 333 Lecture 16 - network programming intro

CSE 333 Lecture 16 - network programming intro CSE 333 Lecture 16 - network programming intro Hal Perkins Department of Computer Science & Engineering University of Washington Today Network programming - dive into the Berkeley / POSIX sockets API -

More information

Introduction and Overview Socket Programming Higher-level interfaces Final thoughts. Network Programming. Samuli Sorvakko/Nixu Oy

Introduction and Overview Socket Programming Higher-level interfaces Final thoughts. Network Programming. Samuli Sorvakko/Nixu Oy Network Programming Samuli Sorvakko/Nixu Oy Telecommunications software and Multimedia Laboratory T-110.4100 Computer Networks October 16, 2008 Agenda 1 Introduction and Overview Introduction 2 Socket

More information

The User Datagram Protocol

The User Datagram Protocol The User Datagram Protocol Stefan D. Bruda Winter 2018 UDP Very similar to the TCP in terms of API Dissimilar with TCP in terms of innards (and hence programming techniques) Many-to-many communication.

More information

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

Message passing systems are popular because they support client-server interactions, where: clients send messages to servers requesting a server. Client-Server Model Message passing systems are popular because they support client-server interactions, where: clients send messages to servers requesting a server. servers provide services requested

More information

NETWORK AND SYSTEM PROGRAMMING

NETWORK AND SYSTEM PROGRAMMING NETWORK AND SYSTEM PROGRAMMING LAB 09 Network Byte Ordering, inet_aton, inet_addr, inet_ntoa Functions Objectives: To learn byte order conversion To understand inet-aton, inet_addr, inet_ntoa Functions

More information

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

CSMC 412. Computer Networks Prof. Ashok K Agrawala Ashok Agrawala Set 2. September 15 CMSC417 Set 2 1 CSMC 412 Computer Networks Prof. Ashok K Agrawala 2015 Ashok Agrawala Set 2 September 15 CMSC417 Set 2 1 Contents Client-server paradigm End systems Clients and servers Sockets Socket abstraction Socket

More information