TCP/IP Protocol Stack IP Device Drivers TCPUDP Application Sockets (Gate to network) TCP: –Establish connection –Maintain connection during the communication.

Slides:



Advertisements
Similar presentations
Socket Programming 101 Vivek Ramachandran.
Advertisements

Introduction to Sockets Jan Why do we need sockets? Provides an abstraction for interprocess communication.
Socket Programming CS3320 Fall 2010.
Programming with UDP – I Covered Subjects: IPv4 Socket Address Structure Byte Ordering Functions Address Access/Conversion Functions Functions: 1.socket()
Socket Programming Application Programming Interface.
CS3516 (B10) HELP Session 2 Presented by Lei Cao.
Quick Overview. 2 ISO/OSI Reference Model Application Application Presentation Presentation Session Session Transport Transport Network Network Data Link.
Socket Programming: a Primer Socket to me!. Feb. 23, 2001EE122, UCB2 Why does one need sockets? application network protocol sockets network.
Windows Sockets Purpose Windows Sockets 2 (Winsock) enables programmers to create advanced internet, intranet, and other network-capable applications to.
1 Socket Interfaces Professor Jinhua Guo CIS527 Fall 2003.
תקשורת באינטרנט Tutorial 8. 2 n Socket programming u What is socket ? u Sockets architecture u Types of Sockets u The Socket system calls u Data Transfer.
Tutorial 8 Socket Programming
CS4514 B05 HELP Session 1 CS4514 – TCP/IP Socket Programming Presented by Feng Li
CS 311 – Lecture 19 Outline Internet Sockets – gethostname utility – struct hostent – inet_addr – Machine byte to Network byte order translation and vice.
UDP: User Datagram Protocol. UDP: User Datagram Protocol [RFC 768] r “bare bones”, “best effort” transport protocol r connectionless: m no handshaking.
CS 311 – Lecture 18 Outline IPC via Sockets – Server side socket() bind() accept() listen() – Client side connect() Lecture 181CS Operating Systems.
Socket Addresses. Domains Internet domains –familiar with these Unix domains –for processes communicating on the same hosts –not sure of widespread use.
CSE/EE 461 Getting Started with Networking. Basic Concepts  A PROCESS is an executing program somewhere.  Eg, “./a.out”  A MESSAGE contains information.
1 Tutorial on Socket Programming Computer Networks - CSC 458 Department of Computer Science Yukun Zhu (Slides are mainly from Monia Ghobadi, and Amin Tootoonchian,
Sockets CIS 370 Fall 2009, UMassD. Introduction  Sockets provide a simple programming interface which is consistent for processes on the same machine.
Socket programming in C. Socket programming Socket API introduced in BSD4.1 UNIX, 1981 explicitly created, used, released by apps client/server paradigm.
CS1652 September 13th, 2012 The slides are adapted from the publisher’s material All material copyright J.F Kurose and K.W. Ross, All Rights.
1 Networking (Stack and Sockets API). 2 Topic Overview Introduction –Protocol Models –Linux Kernel Support TCP/IP Sockets –Usage –Attributes –Example.
Fall 2000Datacom 11 Socket Programming Review Examples: Client and Server-Diagnostics UDP versus TCP Echo.
Operating Systems Chapter 9 Distributed Communication.
Assignment 3 A Client/Server Application: Chatroom.
Socket Programming. Introduction Sockets are a protocol independent method of creating a connection between processes. Sockets can be either – Connection.
9/12/2015B.R1 Socket Abstraction and Interprocess Communication B.Ramamurthy CSE421.
Sockets CIS 370 Lab 10 UMass Dartmouth. Introduction 4 Sockets provide a simple programming interface which is consistent for processes on the same machine.
Sirak Kaewjamnong Computer Network Systems
Server Sockets: A server socket listens on a given port Many different clients may be connecting to that port Ideally, you would like a separate file descriptor.
The Application Layer Application Services (Telnet, FTP, , WWW) Reliable Stream Transport (TCP) Connectionless Packet Delivery Service (IP) Unreliable.
 Wind River Systems, Inc Chapter - 13 Network Programming.
Remote Shell CS230 Project #4 Assigned : Due date :
Lab 5 Sockets. Useful Sockets Links (courtesy of Stanford University) Programming UNIX Sockets in C - Frequently Asked Questions
Socket Programming Lec 2 Rishi Kant. Review of Socket programming Decide which type of socket – stream or datagram. Based on type create socket using.
Networking Tutorial Special Interest Group for Software Engineering Luke Rajlich.
CPSC 441 TUTORIAL – FEB 13, 2012 TA: RUITNG ZHOU UDP REVIEW.
1 Computer Networks An Introduction to Computer Networks University of Tehran Dept. of EE and Computer Engineering By: Dr. Nasser Yazdani Lecture 3: Sockets.
TELE202 Lecture 15 Socket programming 1 Lecturer Dr Z. Huang Overview ¥Last Lecture »TCP/UDP (2) »Source: chapter 17 ¥This Lecture »Socket programming.
Introduction to Socket
CSE/EE 461 Getting Started with Networking. 2 Basic Concepts A PROCESS is an executing program somewhere. –Eg, “./a.out” A MESSAGE contains information.
Socket Programming Lab 1 1CS Computer Networks.
Sockets Socket = abstraction of the port concept: –Application programs request that the operating system create a socket when one is needed –O.S. returns.
2: Application Layer1 Chapter 2: Application layer r 2.1 Principles of network applications r 2.2 Web and HTTP r 2.3 FTP r 2.4 Electronic Mail  SMTP,
CSCI 330 UNIX and Network Programming Unit XV: Transmission Control Protocol.
S OCKET P ROGRAMMING IN C Professor: Dr. Shu-Ching Chen TA: HsinYu Ha.
S OCKET P ROGRAMMING IN C Professor: Dr. Shu-Ching Chen TA: Hsin-Yu Ha.
TCP/IP Protocol Stack IP Device Drivers TCPUDP Application Sockets (Gate to network) TCP: –Establish connection –Maintain connection during the communication.
CSCI 330 UNIX and Network Programming Unit XIV: User Datagram Protocol.
1 Spring Semester 2008, Dept. of Computer Science, Technion Internet Networking recitation #7 Socket Programming.
Lecture 15 Socket Programming CPE 401 / 601 Computer Network Systems slides are modified from Dave Hollinger.
Socket programming in C. Socket programming with TCP Client must contact server server process must first be running server must have created socket (door)
Socket Programming in C CS587x Lecture 3 Department of Computer Science Iowa State University.
1 Socket Interface. 2 Client-Server Architecture The client is the one who speaks first Typical client-server situations  Client and server on the same.
Socket Programming(1/2). Outline  1. Introduction to Network Programming  2. Network Architecture – Client/Server Model  3. TCP Socket Programming.
1 Socket Interface. 2 Basic Sockets API Review Socket Library TCPUDP IP EthernetPPP ARP DHCP, Mail, WWW, TELNET, FTP... Network cardCom Layer 4 / Transport.
Client-Server model. Socket programming 
SOCKET PROGRAMMING Presented By : Divya Sharma.
Assignment 3 A Client/Server Application: Chatroom
Socket programming in C
CS 1652 Jack Lange University of Pittsburgh
Socket Programming in C
Transport layer API: Socket Programming
Things that are nice to know when you’re doing this project
Socket Programming(1/2)
Internet Networking recitation #8
Sockets.
Socket programming in C
Presentation transcript:

TCP/IP Protocol Stack IP Device Drivers TCPUDP Application Sockets (Gate to network) TCP: –Establish connection –Maintain connection during the communication –Release connection –Reliable (Acknowledged, in order) UDP: –No need to setup a link –Independent packets –Not reliable (no acknowledgement)

Programming with Sockets Sockets are Berkeley software distribution UNIX interface to network protocols Endpoint of communication, has a type and one associated process Uses file system model (open-close-read-write) Internet vs. Unix domain

Socket Creation s = socket( domain, type, protocol ) AF_INET AF_UNIX SOCK_STREAM SOCK_DGRAM #include ( 0 ) SOCKET DESCRIPTOR ( int type )

Connection Establishment connect(socket, server address, size of address structure) s = socket pointer returned from socket command Server information, the data structure that keeps this information is : struct sockaddr_in { short int sin_family;//address type(AF_INET) struct in_addr sin_addr;//internet address of server int sin_port;//port number of server }; struct sockaddr_in server; Size of the data structure (struct sockaddr_in)

How to get server address? You may either give the server name (or address) from the command line or paste it directly into the code. There are 2 ways to get the server’s internet address: If the name of the server is given, you can use struct hostent * = gethostbyname(char * name) hostent data type has two fields you will use: struct in_addr h_addr : internet address of server int h_length : length of the address The last step will be to copy the internet address into our server’s address field. Ex. struct hostent *hp; hp = gethostbyname(“sun cise.ufl.edu”); memcpy( (char *) &server.sin_addr, (char *) hp->h_addr, hp->h_length);

How to get server address? The second way is to convert the Internet host address from the standard numbers-and-dots notation into binary data. (ex , this is not the correct ip address of our server, please use your knowledge to find out the ip address of our server, the server’s host name is sun cise.ufl.edu) The function we will use is: int inet_aton( const char * name, struct in_addr * addr); This function converts the Internet host address name from the standard numbers-and-dots notation into binary data and stores it in the struct in_addr that addr points to. inet_aton returns nonzero if the address is valid, zero if not. Ex. Inet_aton(“ ”, &server.sin_addr);

Putting it all together int s; struct sockaddr_in server; struct hostent * hp; s = socket( AF_INET, SOCK_STREAM, 0 ); server.sin_family = AF_INET; hp = gethostbyname( argv[1] ); memcpy( (char *) &server.sin_addr, (char *) hp->h_addr, hp->h_length); server.sin_port = htons( atoi(argv[2]) ); connect( s, (struct sockaddr *) &server, sizeof(server) );

Data Transfer int nbytes = write( int socket, char * data, int length ); It is similar to writing to a file. The first parameter is a pointer to socket, the second parameter is the data you wish to send, and the last parameter is the size of data. Reading from the socket is done using: int nbytes = read( int socket, char * data, int max_length ); nbytes gives you the number of bytes read from the socket. The difference from write is that since we do not know the size of the stream to be received in advanced, we set the third parameter to the maximum length of stream that our data buffer may hold. In case of error both read and write commands return -1.

Data Transfer Example #define MAXMSG 256 char buffer[MAXMSG] ; int nbytes; buffer = “this is a test.”; nbytes = write(s, buffer, strlen(buffer)+1); nbytes = read(s, buffer, MAXMSG);

Closing Connection close( int socket ) SOCKET DESCRIPTOR A SOCK_STREAM socket can be discarded by a close() system call. Ex. close( s );

Client-Server Architecture request response Process request client Host: sun cise.ufl.edu Port: 7000

The Algorithm Set the client id Connect to the server Send the command to the server Get the input Receive the response and print it Close the connection input = “logout” input = “login”

Additional Notes You will use a data format in your application to send to our server. The data format is a data structure with 3 fields. Please be sure to fill all the fields correctly. After you create the PDU, you have to cast it to char pointer in the write command. Ex. struct PDU message; // message is your data structure. You have to set // message.username, message.type, and if necessary // message.payload fields. //now you are ready to send it nbytes = write( s, (char *)&message, sizeof(message) ); What you will read from our server is a string (char *). So, you do not have to do casting. Once you read it to a buffer, just print the string to the standard output. You can refer to read example in slide 9.

Additional Notes We recommend you to use standard C in a UNIX environment. To compile your program you should use the following line: gcc client.c –lsocket –lnsl –o client