Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "CSCE 515: Computer Network Programming Chin-Tser Huang University of South Carolina."— Presentation transcript:

1 CSCE 515: Computer Network Programming Chin-Tser Huang huangct@cse.sc.edu University of South Carolina

2 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

3 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);

4 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

5 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*/

6 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)*/

7 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

8 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

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

10 1/20/200410 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

11 1/20/200411 InetExample.java /* * Java Network Programming, Second Edition * Merlin Hughes, Michael Shoffner, Derek Hamner * Manning Publications Company; ISBN 188477749X * * http://nitric.com/jnp/ * * Copyright (c) 1997-1999 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[]) … }

12 1/20/200412 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) …

13 1/20/200413 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) …

14 1/20/200414 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'; }

15 1/20/200415 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); }

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


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

Similar presentations


Ads by Google