CSCE 515: Computer Network Programming Chin-Tser Huang University of South Carolina.

Slides:



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

Sockets: Network IPC Internet Socket UNIX Domain Socket.
Programming with TCP – I
Today’s topic: Basic TCP API –Socket –Bind –Listen –Connect –Accept –Read –Write –Close.
Socket Programming Application Programming Interface.
Sockets CS 3516 – Computer Networks. Outline Socket basics Socket details (TCP and UDP) Socket options Final notes.
Distributed Computing Systems Sockets. Outline Socket basics Socket details (TCP and UDP) Socket options Final notes.
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.
Networks: TCP/IP Socket Calls1 Elementary TCP Sockets Chapter 4 UNIX Network Programming Vol. 1, Second Ed. Stevens.
Computer Networks Sockets.
Elementary TCP Sockets Chapter 4 UNIX Network Programming Vol. 1, Second Ed. Stevens.
Multimedia Networking Sockets. Outline Socket basics Socket details (TCP and UDP) Socket options Final notes.
Socket Programming: a Primer Socket to me!. Feb. 23, 2001EE122, UCB2 Why does one need sockets? application network protocol sockets network.
Sockets IMGD Outline Socket basics Socket details (TCP and UDP) Socket options Final notes.
CSCE 515: Computer Network Programming Socket Wenyuan Xu Department of Computer Science and Engineering.
Tutorial 8 Socket Programming
Introduction to Socket Programming April What is a socket? An interface between application and network –The application creates a socket –The socket.
Introduction to Project 1 Web Client and Server Jan 2006.
Computer Networks Sockets. Outline F Socket basics F Socket details.
Operating Systems Sockets. Outline F Socket basics F TCP sockets F Socket details F Socket options F Final notes F Project 3.
Some slides are in courtesy of J. Kurose and K. Ross Review of Previous Lecture Electronic Mail: SMTP, POP3, IMAP DNS Socket programming with TCP.
CS 360 – Spring 2007 Pacific University TCP section 6.5 (Read this section!) 27 Feb 2007.
1 Tutorial on Socket Programming Computer Networks - CSC 458 Department of Computer Science Yukun Zhu (Slides are mainly from Monia Ghobadi, and Amin Tootoonchian,
Basic Socket Programming TCP/IP overview. TCP interface Reference: –UNIX Network Programming, by Richard Stevens. –UNIX man page.
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.
Assignment 3 A Client/Server Application: Chatroom.
Elementary TCP Sockets
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.
CSCE 515: Computer Network Programming Chin-Tser Huang University of South Carolina.
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,
Object Oriented Programming in Java Lecture 16. Networking in Java Concepts Technicalities in java.
Remote Shell CS230 Project #4 Assigned : Due date :
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.
TELE202 Lecture 15 Socket programming 1 Lecturer Dr Z. Huang Overview ¥Last Lecture »TCP/UDP (2) »Source: chapter 17 ¥This Lecture »Socket programming.
CSCE 515: Computer Network Programming UDP Socket Wenyuan Xu Department of Computer Science and Engineering.
Elementary TCP Sockets UNIX Network Programming Vol. 1, Second Ed. Stevens Chapter 4.
Introduction to Socket
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.
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()
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.
In unistd.h : int gethostname(char * name, int maxlen) Purpose : Find the computer's name.
1 Spring Semester 2008, Dept. of Computer Science, Technion Internet Networking recitation #7 Socket Programming.
EECS340 Recitation 1: Very helpful to your project Hongyu Gao 1.
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(1/2). Outline  1. Introduction to Network Programming  2. Network Architecture – Client/Server Model  3. TCP Socket Programming.
Assignment 3 A Client/Server Application: Chatroom
Operating Systems Sockets ENCE 360.
Chapter4 Elementary TCP Socket
Socket Programming in C
תקשורת ומחשוב תרגול 3-5 סוקטים ב-C.
TCP Sockets Programming
Socket Programming(1/2)
Sockets Programming Socket to me!.
Sockets Programming Socket to me!.
Internet Networking recitation #8
in unistd.h: int gethostname(char * name, int maxlen)
Sockets.
Today’s topic: Basic TCP API
Presentation transcript:

CSCE 515: Computer Network Programming Chin-Tser Huang University of South Carolina

1/20/20042 Lifecycle of a Stream Socket Server is started and waits for connection requests from clients Client sends a connection request and server accepts the request Client sends more data requests to server, and server sends data replies to client Client closes its end of connection, and server closes its end of connection

1/20/20043 Client-Server Communication (TCP) socket() bind() listen() accept() read() write() read() close() socket() connect() write() read() close() TCP Client TCP Server well-known port blocks until connection from client process request connection establishment data(request) data(reply) end-of-file notification int socket(int family, int type, int protocol); int bind(int sockfd, struct sockaddr *my_addr, int addrlen); int listen(int sockfd, int backlog); int connect(int sockfd, struct sockaddr *serv_addr, int addrlen); int accept(int sockfd, void *addr, int *addrlen); int close(int sockfd); int socket(int family, int type, int protocol);

1/20/20044 Modularized Socket Code: Connecting to a Server (TCP) int connect_socket(char *hostname, int port) { int sock; struct sockaddr_in sin; struct hostent *host; sock = socket(AF_INET, SOCK_STREAM, 0); if (sock == -1) return sock; host = gethostbyname(hostname); if (host == NULL) { close(sock); return -1; } memset (&sin, 0, sizeof(sin)); sin.sin_family = AF_INET; sin.sin_port = htons(port); sin.sin_addr.s_addr = *(unsigned long *)host-> h_addr_list[ 0]; if (connect(sock, (struct sockaddr *) &sin, sizeof(sin)) != 0) { close (sock); return -1; } return sock; } Resolve the host struct hostent *gethostbyname( const char *hostname); /*Return nonnull pointer if OK, NULL on error */ Setup up the struct unit16_t htons(unit16_t host16bitvaule) /*Change the port number from host byte order to network byte order */ Connect connect(int socketfd, const struct sockaddr * servaddr, socket_t addrlen) /*Perform the TCP three way handshaking*/ Hostent structure struct hostent{ char * h_name/*official name of host*/ char ** h_aliases; /* pointer ot array of\ pointers to alias name*/ int h_addrtype /* host address type*/ int h_length/* length of address */ char ** h_addr_list/*ptr to array of ptrs with \ IPv4 or IPv6 address*/ } IPv4 socket address structure struct socketaddr_in{ uint8_t sin_len; /*length of the structure (16)*/ sa_falimily_t sin_family /* AF_INT*/ in_port_t sin_port /* 16 bit TCP or UDP port number*/ struct in_addr sin_addr/* 32 bit Ipv4 address */ char sin_zero(8)/* unused*/ } Make the socket Socket(int family, int type, int protocol); return nonnegative value for OK, -1 for error

1/20/20045 Modularized Socket Code: Listening on a Port (TCP) int make_listen_socket(int port) { struct sockaddr_in sin; int sock; sock = socket(AF_INET, SOCK_STREAM, 0); if (sock < 0) return -1; memset(&sin, 0, sizeof(sin)); sin.sin_family = AF_INET; sin.sin_addr.s_addr = htonl(INADDR_ANY); sin.sin_port = htons(port); if (bind(sock, (struct sockaddr *)&sin, sizeof(sin)) < 0) return -1; return sock; } Make the socket Setup up the struct Bind bind(int sockfd, const struct sockaddr * myaddr, socklen_t addrlen); /* return 0 if OK, -1 on error assigns a local protocol adress to a socket*/

1/20/20046 Modularized Socket Code: Accepting a Client Connection (TCP) int get_client_socket(int listen_socket) { struct sockaddr_in sin; int sock; int sin_len; memset(&sin, 0, sizeof(sin)); sin_len = sizeof(sin); sock = accept(listen_socket, (struct sockaddr *) &sin, &sin_len); return sock; } Setup up the struct Accept the client connection accept(int sockefd, struct sockaddr * claddr, socklen_t * addrlen) /* return nonnegative descriptor if OK, -1 on error return the next completed connection from the front of the completed connection queue. if the queue is empty, the process is put to sleep(assuming blocking socket)*/

1/20/20047 Network Programming in Java Use classes in package java.net which provide access to IP addresses, TCP, UDP, and URL-related mechanisms Use stream-related classes for I/O Use threads for multithreading

1/20/20048 Class InetAddress Constructors No constructors; use getByName(), getByAddress(), getLocalHost(), getAllByName() Static methods InetAddress getLocalHost() throws UnknownHostException InetAddress getByName(String host) throws UnknownHostException InetAddress getByAddress(byte[] addr) throws UnknownHostException InetAddress[] getAllByName(String host) throws UnknownHostException

1/20/20049 Class InetAddress Instance methods byte[] getAddress() String getHostName() String getHostAddress() boolean isMulticastAddress()

1/20/ An InetAddress Example Print out local machine’s address Stay in a loop accepting host names or addresses and looking them up Terminate on EOF

1/20/ InetExample.java /* * Java Network Programming, Second Edition * Merlin Hughes, Michael Shoffner, Derek Hamner * Manning Publications Company; ISBN X * * * * Copyright (c) Merlin Hughes, Michael Shoffner, Derek Hamner; * all rights reserved; see license.txt for details. */ import java.net.*; import java.io.*; public class InetExample { // public static void main (String args[]) … }

1/20/ Method main public static void main (String args[]) { printLocalAddress (); Reader kbd = new FileReader (FileDescriptor.in); BufferedReader bufferedKbd = new BufferedReader (kbd); try { String name; do { System.out.print ("Enter a hostname or IP address: "); System.out.flush (); name = bufferedKbd.readLine (); if (name != null) printRemoteAddress (name); } while (name != null); System.out.println ("exit"); } catch (IOException ex) { System.out.println ("Input error:"); ex.printStackTrace (); } // static void printLocalAddress () … // static void printRemoteAddress (String name) …

1/20/ Method printLocalAddress static void printLocalAddress () { try { InetAddress myself = InetAddress.getLocalHost (); System.out.println ("My name : " + myself.getHostName ()); System.out.println ("My IP : " + myself.getHostAddress ()); System.out.println ("My class : " + ipClass (myself.getAddress ())); } catch (UnknownHostException ex) { System.out.println ("Failed to find myself:"); ex.printStackTrace (); } // static char ipClass (byte[] ip) …

1/20/ Method ipClass static char ipClass (byte[] ip) { int highByte = 0xff & ip[0]; return (highByte < 128) ? 'A' : (highByte < 192) ? 'B' : (highByte < 224) ? 'C' : (highByte < 240) ? 'D' : 'E'; }

1/20/ Method printRemoteAddress static void printRemoteAddress (String name) { try { System.out.println ("Looking up " + name + "..."); InetAddress machine = InetAddress.getByName (name); System.out.println ("Host name : " + machine.getHostName ()); System.out.println ("Host IP : " + machine.getHostAddress ()); System.out.println ("Host class : " + ipClass (machine.getAddress ())); } catch (UnknownHostException ex) { System.out.println ("Failed to lookup " + name); }

1/20/ Next Class Socket programming in Java Read JNP Ch. 14, 16 Project 1 will be passed out