60 likes | 153 Views
CSE 1341 - Honors Principles of Computer Science I. Mark Fontenot mfonten@engr.smu.edu. Note Set 11. Note Set 11 Overview. Networking IP address and Ports Client/Server Ideas Java Networking Server Socket Socket. IP and Ports. Server. IP: 92.113.223.90 Servers run on ports.
E N D
CSE 1341 - HonorsPrinciples of Computer Science I Mark Fontenot mfonten@engr.smu.edu Note Set 11
Note Set 11 Overview • Networking • IP address and Ports • Client/Server Ideas • Java Networking • Server Socket • Socket
IP and Ports Server IP: 92.113.223.90 Servers run on ports IP: 129.119.101.111 Internet Ports: Each computer has port #s between 0 and 65,535. OS usually reserves 1024 for system services (80 for WWW, 21 for FTP)
Server/Client Server: Handles requests from clients serv = create new ServerSocket to listen on some port while (true) { if (serv.iHaveAConnection()) Handle client connection } Client: Connects to Server client = create new socket to IP addr and port while (i have more stuff to do with server){ Send data to server Read data from server } close client
ServerSocketserver; Socket client; try { server = new ServerSocket(8856); client = server.accept(); System.out.println("Got a live one. IP:" + client.getInetAddress().getHostName()); Scanner netReader = new Scanner(client.getInputStream()); System.out.println(netReader.nextLine()); netReader.close(); client.close(); server.close(); } catch (Exception e) { System.out.println(e.getMessage()); } Server Code
Client Code try { //Connect to server Socket client = new Socket ("localhost", 8856); //Create a PrintWriter object that will allow us to //write to the server PrintWriter pw = new PrintWriter(client.getOutputStream()); //send a message to the server using pw pw.println("Hello Server"); pw.close(); client.close(); } catch (Exception e) { System.out.println(e.getMessage()); }