Socket programming case study NCKU Multimedia Networking Lab Pei Chuan Liu.

Slides:



Advertisements
Similar presentations
Introduction to Sockets Jan Why do we need sockets? Provides an abstraction for interprocess communication.
Advertisements

ZSocket address structure in : struct sockaddr{ u_shortsa_family;/* address family: AF_xxx value */ charsa_data[14];/* up to 14 bytes of protocol- */ /*
Socket Programming Application Programming Interface.
Björn Landfeldt School of Information Technologies NETS3303 Network Programming Intro.
Elementary TCP Sockets Computer Networks Computer Networks Term B10 UNIX Network Programming Vol. 1, Second Ed. Stevens Chapter 4.
Socket programming Tasos Alexandridis 1HY335a - Socket Programming.
Sockets Programming CS144 Review Session 1 April 4, 2008 Ben Nham.
Socket Programming: a Primer Socket to me!. Feb. 23, 2001EE122, UCB2 Why does one need sockets? application network protocol sockets network.
Network Programming UNIX Internet Socket API. Everything in Unix is a File –When Unix programs do any sort of I/O, they do it by reading or writing to.
תקשורת באינטרנט 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
TDC561 Network Programming Camelia Zlatea, PhD Week 2 – part II: Socket Application Programming Interface.
CSCE 515: Computer Network Programming Chin-Tser Huang University of South Carolina.
2: Application Layer1 Socket programming Socket API r introduced in BSD4.1 UNIX, 1981 r explicitly created, used, released by apps r client/server paradigm.
1) The server should be concurrent. This implies that it should loop infinitely, listening for clients requests. It should NOT terminate after accepting.
TCP Socket Programming. r An abstract interface provided to the application programmer  File descriptor, allows apps to read/write to the network r Allows.
Yu-Chi Lai Lecture 3 Network Programming CS 640: Computer Networking.
Ashutosh Shukla Lecture 3 Network Programming CS 640: Computer Networking.
UNIX Socket Programming CS 6378
Elementary UDP Sockets© Dr. Ayman Abdel-Hamid, CS4254 Spring CS4254 Computer Network Architecture and Programming Dr. Ayman A. Abdel-Hamid Computer.
Socket Programming (C/Java)
TCP Socket Programming. r An abstract interface provided to the application programmer  File descriptor, allows apps to read/write to the network r Allows.
ECE453 – Introduction to Computer Networks Lecture 15 – Transport Layer (II)
ECE 4110 – Internetwork Programming Client-Server Model.
Computer Networks Lecture 1 Adrian Sergiu DARABANT.
Assignment 3 A Client/Server Application: Chatroom.
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.
Sirak Kaewjamnong Computer Network Systems
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 :
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.
Cli/Serv.: sockets 3/91 Client/Server Distributed Systems v Objectives –describe iterative clients and servers using the UDP protocol ,
Elementary TCP Sockets UNIX Network Programming Vol. 1, Second Ed. Stevens Chapter 4.
Introduction to Socket
Sockets Socket = abstraction of the port concept: –Application programs request that the operating system create a socket when one is needed –O.S. returns.
Chapter 2 Applications and Layered Architectures Sockets.
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.
Intro to Socket Programming CS 360. Page 2 CS 360, WSU Vancouver Two views: Server vs. Client Servers LISTEN for a connection and respond when one is.
UNIX Internet Socket API
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.
Socket Programming. Computer Science, FSU2 Interprocess Communication Within a single system – Pipes, FIFOs – Message Queues – Semaphores, Shared Memory.
1 Spring Semester 2008, Dept. of Computer Science, Technion Internet Networking recitation #7 Socket Programming.
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.
Socket Programming Jignesh Patel Palanivel Rathinam connecting processes.
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.
Socket Programming Client/Server.
Assignment 3 A Client/Server Application: Chatroom
Socket programming Péter Verhás August 2002
Socket Programming in C
CSCD433 Advanced Networks Spring 2016 Lecture 16a
Transport layer API: Socket Programming
תקשורת ומחשוב תרגול 3-5 סוקטים ב-C.
Berkeley API Socket Programming
Socket Programming in C
Socket Programming.
Berkeley API Socket Programming
Yu-Chi Lai Lecture 3 Network Programming
Socket Programming(1/2)
Internet Networking recitation #8
Berkeley API Socket Programming
Presentation transcript:

Socket programming case study NCKU Multimedia Networking Lab Pei Chuan Liu

Socket programming With C int connect(int sockfd, struct sockaddr *serv_addr, int addrlen); int listen(int s, int backlog); int accept(int s, struct sockaddr *addr, int *addrlen); int send(int s, const void *msg, int len, unsigned int flags); int sendto(int s, const void *msg, int len, unsigned int flags, const struct sockaddr *to, int tolen); int recv(int s, void *buf, int len, unsigned int flags); int recvfrom(int s, void *buf, int len, unsigned int flags struct sockaddr *from, int *fromlen);

#include #include /* close */ #define SERVER_PORT 1500 #define MAX_MSG 100 int main (int argc, char *argv[]) { int sd, rc, i; struct sockaddr_in localAddr, servAddr; struct hostent *h; if(argc < 3) { printf("usage: %s... \n",argv[0]); exit(1); } h = gethostbyname(argv[1]); if(h==NULL) { printf("%s: unknown host '%s'\n",argv[0],argv[1]); exit(1); } servAddr.sin_family = h->h_addrtype; memcpy((char *) &servAddr.sin_addr.s_addr, h->h_addr_list[0], h->h_length); servAddr.sin_port = htons(SERVER_PORT); /* create socket */ sd = socket(AF_INET, SOCK_STREAM, 0); if(sd<0) { perror("cannot open socket "); exit(1); } UDP Server

/* bind any port number */ localAddr.sin_family = AF_INET; localAddr.sin_addr.s_addr = htonl(INADDR_ANY); localAddr.sin_port = htons(0); rc = bind(sd, (struct sockaddr *) &localAddr, sizeof(localAddr)); if(rc<0) { printf("%s: cannot bind port TCP %u\n",argv[0],SERVER_PORT); perror("error "); exit(1); } /* connect to server */ rc = connect(sd, (struct sockaddr *) &servAddr, sizeof(servAddr)); if(rc<0) { perror("cannot connect "); exit(1); } for(i=2;i<argc;i++) { rc = send(sd, argv[i], strlen(argv[i]) + 1, 0); if(rc<0) { perror("cannot send data "); close(sd); exit(1); } printf("%s: data%u sent (%s)\n",argv[0],i-1,argv[i]); } return 0; }

#include #include /* memset() */ #include /* select() */ #define REMOTE_SERVER_PORT 1500 #define MAX_MSG 100 int main(int argc, char *argv[]) { int sd, rc, i; struct sockaddr_in cliAddr, remoteServAddr; struct hostent *h; /* check command line args */ if(argc<3) { printf("usage : %s... \n", argv[0]); exit(1); } /* get server IP address (no check if input is IP address or DNS name */ h = gethostbyname(argv[1]); if(h==NULL) { printf("%s: unknown host '%s' \n", argv[0], argv[1]); exit(1); } printf("%s: sending data to '%s' (IP : %s) \n", argv[0], h->h_name, inet_ntoa(*(struct in_addr *)h->h_addr_list[0])); remoteServAddr.sin_family = h->h_addrtype; memcpy((char *) &remoteServAddr.sin_addr.s_addr, h->h_addr_list[0], h->h_length); remoteServAddr.sin_port = htons(REMOTE_SERVER_PORT); UDP Client

/* socket creation */ sd = socket(AF_INET,SOCK_DGRAM,0); if(sd<0) { printf("%s: cannot open socket \n",argv[0]); exit(1); } /* bind any port */ cliAddr.sin_family = AF_INET; cliAddr.sin_addr.s_addr = htonl(INADDR_ANY); cliAddr.sin_port = htons(0); rc = bind(sd, (struct sockaddr *) &cliAddr, sizeof(cliAddr)); if(rc<0) { printf("%s: cannot bind port\n", argv[0]); exit(1); } /* send data */ for(i=2;i<argc;i++) { rc = sendto(sd, argv[i], strlen(argv[i])+1, 0, (struct sockaddr *) &remoteServAddr, sizeof(remoteServAddr)); if(rc<0) { printf("%s: cannot send data %d \n",argv[0],i-1); close(sd); exit(1); } return 1; }

#include #include /* close */ #define SUCCESS 0 #define ERROR 1 #define END_LINE 0x0A #define SERVER_PORT 1500 #define MAX_MSG 100 /* function readline */ int read_line(); int main (int argc, char *argv[]) { int sd, newSd, cliLen; struct sockaddr_in cliAddr, servAddr; char line[MAX_MSG]; /* create socket */ sd = socket(AF_INET, SOCK_STREAM, 0); if(sd<0) { perror("cannot open socket "); return ERROR; } /* bind server port */ servAddr.sin_family = AF_INET; servAddr.sin_addr.s_addr = htonl(INADDR_ANY); servAddr.sin_port = htons(SERVER_PORT); TCP Server

if(bind(sd, (struct sockaddr *) &servAddr, sizeof(servAddr))<0) { perror("cannot bind port "); return ERROR; } listen(sd,5); while(1) { printf("%s: waiting for data on port TCP %u\n",argv[0],SERVER_PORT); cliLen = sizeof(cliAddr); newSd = accept(sd, (struct sockaddr *) &cliAddr, &cliLen); if(newSd<0) { perror("cannot accept connection "); return ERROR; } /* init line */ memset(line,0x0,MAX_MSG); /* receive segments */ while(read_line(newSd,line)!=ERROR) { printf("%s: received from %s:TCP%d : %s\n", argv[0], inet_ntoa(cliAddr.sin_addr), ntohs(cliAddr.sin_port), line); /* init line */ memset(line,0x0,MAX_MSG); } /* while(read_line) */ } /* while (1) */ }

/* WARNING WARNING WARNING WARNING WARNING WARNING WARNING */ /* this function is experimental.. I don't know yet if it works */ /* correctly or not. Use Steven's readline() function to have */ /* something robust. */ /* WARNING WARNING WARNING WARNING WARNING WARNING WARNING */ /* rcv_line is my function readline(). Data is read from the socket when */ /* needed, but not byte after bytes. All the received data is read. */ /* This means only one call to recv(), instead of one call for */ /* each received byte. */ /* You can set END_CHAR to whatever means endofline for you. (0x0A is \n)*/ /* read_lin returns the number of bytes returned in line_to_return */ int read_line(int newSd, char *line_to_return) { static int rcv_ptr=0; static char rcv_msg[MAX_MSG]; static int n; int offset; offset=0; while(1) { if(rcv_ptr==0) { /* read data from socket */ memset(rcv_msg,0x0,MAX_MSG); /* init buffer */ n = recv(newSd, rcv_msg, MAX_MSG, 0); /* wait for data */ if (n<0) { perror(" cannot receive data "); return ERROR; } else if (n==0) { printf(" connection closed by client\n"); close(newSd); return ERROR; } /* if new data read on socket */ /* OR */ /* if another line is still in buffer */

/* copy line into 'line_to_return' */ while(*(rcv_msg+rcv_ptr)!=END_LINE && rcv_ptr<n) { memcpy(line_to_return+offset,rcv_msg+rcv_ptr,1); offset++; rcv_ptr++; } /* end of line + end of buffer => return line */ if(rcv_ptr==n-1) { /* set last byte to END_LINE */ *(line_to_return+offset)=END_LINE; rcv_ptr=0; return ++offset; } /* end of line but still some data in buffer => return line */ if(rcv_ptr <n-1) { /* set last byte to END_LINE */ *(line_to_return+offset)=END_LINE; rcv_ptr++; return ++offset; } /* end of buffer but line is not ended => */ /* wait for more data to arrive on socket */ if(rcv_ptr == n) { rcv_ptr = 0; } } /* while */ }

TCP Client #include #include /* close */ #define SERVER_PORT 1500 #define MAX_MSG 100 int main (int argc, char *argv[]) { int sd, rc, i; struct sockaddr_in localAddr, servAddr; struct hostent *h; if(argc < 3) { printf("usage: %s... \n",argv[0]); exit(1); } h = gethostbyname(argv[1]); if(h==NULL) { printf("%s: unknown host '%s'\n",argv[0],argv[1]); exit(1); } servAddr.sin_family = h->h_addrtype; memcpy((char *) &servAddr.sin_addr.s_addr, h->h_addr_list[0], h->h_length); servAddr.sin_port = htons(SERVER_PORT); /* create socket */ sd = socket(AF_INET, SOCK_STREAM, 0); if(sd<0) { perror("cannot open socket "); exit(1); }

/* bind any port number */ localAddr.sin_family = AF_INET; localAddr.sin_addr.s_addr = htonl(INADDR_ANY); localAddr.sin_port = htons(0); rc = bind(sd, (struct sockaddr *) &localAddr, sizeof(localAddr)); if(rc<0) { printf("%s: cannot bind port TCP %u\n",argv[0],SERVER_PORT); perror("error "); exit(1); } /* connect to server */ rc = connect(sd, (struct sockaddr *) &servAddr, sizeof(servAddr)); if(rc<0) { perror("cannot connect "); exit(1); } for(i=2;i<argc;i++) { rc = send(sd, argv[i], strlen(argv[i]) + 1, 0); if(rc<0) { perror("cannot send data "); close(sd); exit(1); } printf("%s: data%u sent (%s)\n",argv[0],i-1,argv[i]); } return 0; }

Socket programming with Java Class ServerSocket – java.lang.Object |  java.net.ServerSocket java.lang.Object public ServerSocket(int port, int backlog, InetAddress bindAddr) throws IOException InetAddressIOException Class Socket – java.lang.Object java.lang.Object  java.net.Socket public Socket(InetAddress host, int port, boolean stream) throws IOExceptionInetAddressIOException

class SimpleWebServer { public static void main(String args[]) { ServerSocket serverSocket = null; Socket clientSocket = null; int connects = 0; try { // Create the server socket serverSocket = new ServerSocket(80, 5); while (connects < 5) { // Wait for a connection clientSocket = serverSocket.accept(); //Service the connection ServiceClient(clientSocket); connects++; } serverSocket.close(); } catch (IOException ioe) { System.out.println("Error in SimpleWebServer: " + ioe); } public static void ServiceClient(Socket client) throws IOException { DataInputStream inbound = null; DataOutputStream outbound = null; try { // Acquire the streams for IO inbound = new DataInputStream( client.getInputStream()); outbound = new DataOutputStream( client.getOutputStream()); Simple Web Server App.

// Format the output (response header and tiny HTML document) StringBuffer buffer = PrepareOutput(); String inputLine; while ((inputLine = inbound.readLine()) != null) { // If end of HTTP request, send the response if ( inputLine.equals("") ) { outbound.writeBytes(buffer.toString()); break; } finally { // Clean up System.out.println("Cleaning up connection: " + client); outbound.close(); inbound.close(); client.close(); }