Download presentation
Presentation is loading. Please wait.
1
Network Programming CS3250
2
References Core Java, Vol. II, Chapter 3. Book examples are available from http://horstmann.com/corejava.html http://horstmann.com/corejava.html Web site for Practical TCP/IP Sockets in Java by Kenneth L. Calvert and Michael J. Donahoo. Slides and sample code: http://cs.ecs.baylor.edu/~donahoo/practical/ JavaSockets/ http://cs.ecs.baylor.edu/~donahoo/practical/ JavaSockets/ The Java Tutorial: http://java.sun.com/docs/books/tutorial/net working/index.html http://java.sun.com/docs/books/tutorial/net working/index.html
3
The Server Server The server starts running first and waits for clients to connect. ServerSocket s = new ServerSocket(8189); Socket incoming = s.accept(); Port number This socket is only used to get new connections to clients. The server does not read from or write to this socket. from EchoServer.java, Core Java Vol. II, Ch. 3, pp. 178-179 (8 th ed.)
4
Accepting Connections Server The server starts running first and waits for clients to connect. ServerSocket s = new ServerSocket(8189); Socket incoming = s.accept(); Each time a client connects, the server gets a new socket which it uses only to communicate with that client. from EchoServer.java, Core Java Vol. II, Ch. 3, pp. 178-179 (8 th ed.) Client programs do not use the ServerSocket class or call accept.
5
The Client Server Client All transactions are initiated by clients. The client must know the name (or IP address) and port number of the server. Socket s = new Socket("localhost", 8189);
6
Input and Output Streams Server Client The client gets the input and output streams from the socket to receive and send to the server. Socket s = new Socket("localhost", 8189); OutputStream outStream = s.getOutputStream(); PrintWriter out = new PrinterWriter (outStream, true); InputStream inStream = s.getInputStream(); Scanner in = new Scanner(inStream);
7
Sending to the Server Server Client The client uses the output stream to send a request to the server. Request out.println("connect Gimli");
8
Receiving a Reply Server Client The client uses the input stream to receive replies from the server. Reply String reply = in.nextLine(); The nextLine method will block until the server sends a reply. If the program only has one thread, the whole program will be blocked, which is usually not good.
9
Using Threads Server Client Messages from the server Messages to the server Thread 1 Thread 2 User I/O
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.