Lab 5 Sockets. Useful Sockets Links (courtesy of Stanford University) Programming UNIX Sockets in C - Frequently Asked Questions

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.
Sockets: Network IPC Internet Socket UNIX Domain Socket.
Networks: TCP/IP Socket Calls1 Elementary TCP Sockets Chapter 4 UNIX Network Programming Vol. 1, Second Ed. Stevens.
Elementary TCP Sockets Chapter 4 UNIX Network Programming Vol. 1, Second Ed. Stevens.
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
Programming with Berkeley Sockets Presented by Chris GauthierDickey Written by Daniel Stutzbach (I think!) for CIS 432/532 Useful References: ● man pages.
1 Tutorial on Socket Programming Computer Networks - CSC 458 Department of Computer Science Yukun Zhu (Slides are mainly from Monia Ghobadi, and Amin Tootoonchian,
UNIX Sockets COS 461 Precept 1.
TCP Socket Programming. r An abstract interface provided to the application programmer  File descriptor, allows apps to read/write to the network r Allows.
ECE 4110 – Internetwork Programming Client-Server Model.
Sockets and intro to IO multiplexing. Goals We are going to study sockets programming as means to introduce IO multiplexing problem. We will revisit socket.
TCP/IP Protocol Stack IP Device Drivers TCPUDP Application Sockets (Gate to network) TCP: –Establish connection –Maintain connection during the communication.
Assignment 3 A Client/Server Application: Chatroom.
Network Programming Tutorial #9 CPSC 261. A socket is one end of a virtual communication channel Provides network connectivity to any other socket anywhere.
Socket Programming. Introduction Sockets are a protocol independent method of creating a connection between processes. Sockets can be either – Connection.
Zhu Reference: Daniel Spangenberger Computer Networks, Fall 2007 PPT-4 Socket Programming.
CS345 Operating Systems Φροντιστήριο Άσκησης 2. Inter-process communication Exchange data among processes Methods –Signal –Pipe –Sockets.
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.
CS162B: IPv4 Socketing Jacob T. Chan. Socketing in the Real World  Most computer games are multiplayer in nature or have multiplayer components  DotA,
---- IT Acumens. COM IT Acumens. COMIT Acumens. COM.
Ports Port - A 16-bit number that identifies the application process that receives an incoming message. Reserved ports or well-known ports (0 to 1023)
Remote Shell CS230 Project #4 Assigned : Due date :
Technical Details for sockaddr_in Structure Department of Computer Science Southern Illinois University Edwardsville Fall, 2015 Dr. Hiroshi Fujinoki
1 COMP445 Fall 2006 Lab assignment 1. 2 STEP1: Get the name of the second party STEP2: Get phone number from white pages CALLERRECEIVER STEP1: Plug the.
Networking Tutorial Special Interest Group for Software Engineering Luke Rajlich.
UNIX Sockets COS 461 Precept 1. Socket and Process Communication The interface that the OS provides to its networking subsystem application layer transport.
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 Tutorial Department of Computer Science Southern Illinois University Edwardsville Fall, 2015 Dr. Hiroshi Fujinoki
Socket Programming Lab 1 1CS Computer Networks.
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,
Programming with UDP – II Covered Subjects: Creating UDP sockets Client Server Sending data Receiving data Connected mode.
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.
Introduction to Sockets
S OCKET P ROGRAMMING IN C Professor: Dr. Shu-Ching Chen TA: Hsin-Yu Ha.
Read() recv() connection establishment Server (connection-oriented protocol) blocks until connection from client Client socket() bind() listen() accept()
2: Application Layer 1 Socket Programming UNIX Network Programming, Socket Programming Tutorial:
CSCI 330 UNIX and Network Programming Unit XIV: User Datagram Protocol.
回到第一頁 Client/sever model n Client asks (request) – server provides (response) n Typically: single server - multiple clients n The server does not need.
Socket Programming. Computer Science, FSU2 Interprocess Communication Within a single system – Pipes, FIFOs – Message Queues – Semaphores, Shared Memory.
UNIX Sockets Outline UNIX sockets CS 640.
1 Spring Semester 2008, Dept. of Computer Science, Technion Internet Networking recitation #7 Socket Programming.
1 TCP Sockets Programming Creating a passive mode (server) socket.Creating a passive mode (server) socket. Establishing an application-level connection.Establishing.
CS 447 Networks and Data Communication Server-Process Organization IP address and SockAddr_In Data Structure Department of Computer Science Southern Illinois.
Lecture 3 TCP and UDP Sockets CPE 401 / 601 Computer Network Systems slides are modified from Dave Hollinger.
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.
Sockets Intro to Network Programming. Before the internet... Early computers were entirely isolated No Internet No network No model No external communications.
1 Socket Interface. 2 Basic Sockets API Review Socket Library TCPUDP IP EthernetPPP ARP DHCP, Mail, WWW, TELNET, FTP... Network cardCom Layer 4 / Transport.
CS 3700 Networks and Distributed Systems
UNIX Sockets COS 461 Precept 1.
Socket Programming in C
Tutorial on Socket Programming
Transport layer API: Socket Programming
CS 3700 Networks and Distributed Systems
Things that are nice to know when you’re doing this project
Recitation 11 – 4/29/01 Outline Sockets Interface
UNIX Sockets Outline Homework #1 posted by end of day
Socket Programming in C
TCP Sockets Programming
Chapter 3 Socket API © Bobby Hoggard, Department of Computer Science, East Carolina University These slides may not be used or duplicated without permission.
Socket Programming(1/2)
Chapter 04. TCP Server/Client.
Internet Networking recitation #8
Presentation transcript:

Lab 5 Sockets

Useful Sockets Links (courtesy of Stanford University) Programming UNIX Sockets in C - Frequently Asked Questions /unix-socket-faq/unix-socket-faq.html /unix-socket-faq/unix-socket-faq.html Sockets Programming Socket Interface chap22/chap22_0.htmlhttp:// chap22/chap22_0.html BSD Sockets: A Quick and Dirty Primer Beej's Guide to Network Programming Sockets Tutorial

Compiling Socket Code #include gcc -lsocket -lnsl -g -Wall -o foo foo.c

Outgoing Socket #include #define HTTP_PORT 80 int main() { int our_socket; struct sockaddr_in serverAddr; struct hostent *destination; destination = gethostbyname("ftp.redhat.com"); //Also works with “ ” format if(destination==NULL) { printf("No such host\n"); exit(1); } Two Data Structures One will get filled in for us.. We'll have to copy info in to the other...

gethostbyname struct hostent *gethostbyname(const char *name); People operate the Internet with names.. i.e. " "ftp.redhat.com" The Internet itself operates using 32 bit numbers to identify computers. These numbers are known as Internet Protocol (IP) addresses. gethostbyname() finds, builds and returns address of a data structure with information on the web site/ftp site with the specified name

Outgoing Socket (cont'd) //now we have to copy info from //one data structure to another //That's all these 2 lines do... serverAddr.sin_family = destination->h_addrtype; memcpy( (char *)&serverAddr.sin_addr.s_addr, destination->h_addr_list[0], destination->h_length ); What does the memcpy function do? What are its parameters?

Ports Each IP address has 65,536 "Ports" associated with it. Some of these ports have standardized uses: PortUse 21FTP Server (Control) 22SSH Server 80HTTP Server 443Secure HTTP (TLS/SSL) We have to select the port number that we want to access on the server: serverAddr.sin_port = htons(HTTP_PORT);

Outgoing Socket serverAddr.sin_port = htons(HTTP_PORT); //htons = 'host to network' //remember big and little endian? our_socket = socket(AF_INET, SOCK_STREAM, 0); if(our_socket < 0) { printf("cannot open socket\n"); exit(1); }

Connecting Outgoing Socket int server_response; server_response = connect(our_socket, (struct sockaddr *) &serverAddr, sizeof(serverAddr)); if(server_response < 0) { printf("cannot connect\n"); exit(1); }

Connect int connect(int sockfd, const struct sockaddr *serv_addr, socklen_t addrlen);

Reading from Socket char temp; int bytes_read; while (1) { bytes_read = recv(our_socket, &temp, 1, 0); if (bytes_read != 1) { break; } printf (“%c\n”, temp); }

Receiving Data Returns -1 if unsuccessful. Otherwise number of bytes read. Must pass in pointer to a buffer that you want to read in to and the size of that buffer. size_t recv (int socket, void *buffer, size_t buffer_size, int flags) You can cast an int to a size_t for buffer_size You can set additional flags or use 0 for flags

Sending Data int bytes_sent; char request[18]= “GET / HTTP/1.0\012\015\012\015”; bytes_sent = send(our_socket, request, strlen(request), 0); if (bytes_sent != strlen(request)) { printf("We didn't send the command properly\n"); return 0; }

Sending Data Returns -1 if unsuccessful. Otherwise number of bytes sent. Must pass in pointer to a buffer that you want to send and the size of that buffer. size_t send (int socket, void *buffer, size_t buffer_size, int flags) You can cast an int to a size_t for buffer_size You can set additional flags or use 0 for flags

Closing the Socket shutdown(our_socket,SHUT_RDWR); SHUT_RD = No more receptions; SHUT_WR = No more transmissions; SHUT_RDWR = No more receptions or transmissions.

Incoming Socket #define DATA_PORT 5999 int our_socket; struct sockaddr_in localAddr; /* Set up data socket */ localAddr.sin_family = AF_INET; localAddr.sin_addr.s_addr = htonl(INADDR_ANY); localAddr.sin_port = htons(DATA_PORT);

Creating Incoming Socket our_socket = socket(AF_INET, SOCK_STREAM, 0);

Binding Incoming Address int response; response = bind(our_socket, (struct sockaddr *) &localAddr, sizeof(localAddr)); if(response < 0) { exit(1); }

Listening for Incoming int response, N=1; response = listen(our_socket, N); if (response != 0) { printf("Cannot listen on port\n"); } Prepare to accept connections on socket. N connection requests will be queued before further requests are refused. Returns 0 on success, -1 for errors extern int listen (int socket, int N);

Accept Incoming Socket int nsock; nsock = accept(our_socket, 0, 0); int accept (int listening_socket, struct sockaddr * address, socklen_t * address_length);

FTP Server Computer Lab Computer X (x is a number) Port 21 Control Connection Data Connection Commands Your File