Download presentation
Presentation is loading. Please wait.
Published byJulie Grant Modified over 9 years ago
1
Mark Fontenot mfonten@engr.smu.edu CSE 1341 - Honors Principles of Computer Science I Note Set 11
2
Note Set 11 Overview Networking IP address and Ports Client/Server Ideas Java Networking Server Socket Socket
3
IP and Ports Internet IP: 129.119.101.111 IP: 92.113.223.90 Servers run on ports Server Ports: Each computer has port #s between 0 and 65,535. OS usually reserves 1024 for system services (80 for WWW, 21 for FTP)
4
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
5
ServerSocket server; 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
6
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()); } Client Code
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.