Download presentation
Presentation is loading. Please wait.
1
Björn Landfeldt School of Information Technologies NETS3303 Network Programming Intro
2
Björn Landfeldt School of Information Technologies Distributed Programming Model Master-Terminal –Fill dumb terminal with code to execute on server (mainly extinct) Client-Server –Two independent pieces of code working together, Different roles (most common) P-2-P –All nodes equal, both client and server (on the rise)
3
Björn Landfeldt School of Information Technologies Client - Server Process 1 Process 2Matching communication End-points
4
Björn Landfeldt School of Information Technologies P-2-P
5
Björn Landfeldt School of Information Technologies Centralised P-2-P
6
Björn Landfeldt School of Information Technologies De-Centralised P-2-P
7
Björn Landfeldt School of Information Technologies Fundamentals Processes exchange data –Need pipe from process to stack –Need address to other process –Need knowledge of how data is represented and agreement between processes Things happen in –User space –Kernel space
8
Björn Landfeldt School of Information Technologies Pipe A process typically open up a pipe with a descriptor (compare file descriptor) as an end-point for communication Process reads from and writes to pipe
9
Björn Landfeldt School of Information Technologies Addressing How can we find the communication end point (pipe) of the remote process? In TCP/IP three address components –IP address –Protocol –Port Called a Socket!
10
Björn Landfeldt School of Information Technologies Addressing Protocol IP address 1 Ports Process 1 Process 2 Protocol IP address 2 Ports Process 1 Process 2
11
Björn Landfeldt School of Information Technologies Data representation So now we know –Send data to IP x, Proto Y, Port z How do we interpret data? How do we know what data to expect? How do we know when things go right or wrong?
12
Björn Landfeldt School of Information Technologies Protocols Protocols are informal agreements of –When we can send data –How data is represented 8-bit, 16-bit etc. –How to interpret data / react For now –Transport protocols (UDP, TCP) –Application protocols (HTTP, SMTP, GNUTELLA)
13
Björn Landfeldt School of Information Technologies Client or server? A client actuates initial communication. Before this happens Server has to listen for connection attempts
14
Björn Landfeldt School of Information Technologies Client Open up connection to remote end point (socket) Send some initial data (Maybe) start listening for a reply In all APIs this lecturer knows of, sockets are two-way, you can both read and write
15
Björn Landfeldt School of Information Technologies JAVA Client import java.lang.*; import java.io.*; import java.net.*; class Client { public static void main(String args[]) { try { Socket skt = new Socket("localhost", 1234); networkPout = new PrintWriter(skt.getOutputStream(),true); networkPout.println(“Hello World”); } catch(Exception e) { System.out.print("Whoops! It didn't work!\n"); } Not complete!!!
16
Björn Landfeldt School of Information Technologies Interpretation Class Socket = TCP, DatagramSocket = UDP Socket(“localhost”, 1234), localhost = IPaddress (sort of), 1234 = Port PrintWriter = text filter via OutputStream = pipe to socket. Try, catch = (you know this)
17
Björn Landfeldt School of Information Technologies JAVA Server import java.net.ServerSocket; import java.net.Socket; import java.io.IOException; import java.io.InputStreamReader; import java.io.BufferedReader; import java.lang.System; public class server { public static void main (String args[]) throws IOException { ServerSocket s = new ServerSocket (1234); //TCP server socket Socket sock; BufferedReader datain; System.out.println ("Server starting..."); sock = s.accept (); //Sit and wait datain = new BufferedReader (new InputStreamReader (sock.getInputStream ())); System.out.println (datain.readLine ()); // Read message and print out sock.close (); s.close (); }
18
Björn Landfeldt School of Information Technologies Iterative - Concurrent Server Last example Iterative –Handle each connection fully before –closing socket and –Go back to listening Concurrent –Handle connection –Spawn new process or thread –Go back to listening
19
Björn Landfeldt School of Information Technologies Concurrent public class server { public static void main (String args[]) throws IOException { ServerSocket s = new ServerSocket (1234); //TCP server socket Socket sock; BufferedReader datain; while(true) { sock = s.accept (); //Sit and wait new sockDealer(sock).start(): } public class sockDealer extends Thread { sockDealer(Socket sock) { new Socket s = sock; } public void run() { try { read data & process whatever from s; } catch(IOException e){} }
20
Björn Landfeldt School of Information Technologies Hint on objects How send complex structures? Objects with states? Arrays, vectors, matrices, hashtables? Serialize public class UserData implements java.io.Serializable
21
Björn Landfeldt School of Information Technologies C - client #include #include /* close */ #define SERVER_PORT 1500 struct sockaddr_in localAddr, servAddr; struct hostent *h; h = gethostbyname(“www.it.usyd.edu.au); if(h==NULL) { printf("%s: unknown host '); exit(1); } /* Fill struct with values *? 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; // IPv4 localAddr.sin_addr.s_addr = htonl(INADDR_ANY); localAddr.sin_port = htons(0);
22
Björn Landfeldt School of Information Technologies C - client 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); } rc = send(data); if(rc<0) { perror("cannot send data "); close(sd); exit(1); }
23
Björn Landfeldt School of Information Technologies C-Server /* 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); 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);
24
Björn Landfeldt School of Information Technologies Concurrent C-Server /* // Fork process into 2 processes. // - Parent server process will continue to // listen for other client connection requests. // - Child server process will serve the currently connected client. */ if ((process_id = fork()) < 0) { perror("fork"); exit(EXIT_FAILURE); } /* // Parent server process: // - Close current connection. // - Continue listening for more client connections */ if (process_id > 0) { close(current_socket); continue; } /* // Child server process: // - Close server socket. // - Serve current connection. */ close(server_socket); serve_connection(current_socket); } return EXIT_SUCCESS; } void serve_connection ( int socket_value )
25
Björn Landfeldt School of Information Technologies Loopback interface Use for local testing Run server and client on same host 127.0.0.1 or “localhost” Hint: Select high port numbers for testing (above 10000)
26
Björn Landfeldt School of Information Technologies
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.