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

Slides:



Advertisements
Similar presentations
Lecture 2b Sockets Erick Pranata © Sekolah Tinggi Teknik Surabaya 1.
Advertisements

Sockets for Clients Socket Basics  Connect to a remote machine  Send data  Receive data  Close a connection  Bind to a port 
Referring to Java API Specifications
Socket Programming By Ratnakar Kamath. What Is a Socket? Server has a socket bound to a specific port number. Client makes a connection request. Server.
Lecture 3: Sockets, Remote Procedure Calls. EECE 411: Design of Distributed Software Applications Last time: Pitfalls when developing distributed systems.
© 2003 Andrea Calvagna 5/16/2015 Java Sockets and Server Sockets Low Level Network Programming Andrea Calvagna
© 1999 Elliotte Rusty Harold 5/16/2015 Java Sockets and Server Sockets Low Level Network Programming Elliotte Rusty Harold
Programming TCP Clients Version InetAddress Class An IP address identifies uniquely a host in the internet, which consists of 4 numbers (1 byte.
Prepared By E. Musa Alyaman1 Java Network Programming TCP.
User Datagram Protocol. Introduction UDP is a connectionless transport protocol, i.e. it doesn't guarantee either packet delivery or that packets arrive.
CSCE 515: Computer Network Programming Chin-Tser Huang University of South Carolina.
CSCE 515: Computer Network Programming Chin-Tser Huang University of South Carolina.
WECPP1 Java networking Jim Briggs based on notes by Amanda Peart based on Bell & Parr's bonus chapter
1 L53 Networking (2). 2 OBJECTIVES In this chapter you will learn:  To understand Java networking with URLs, sockets and datagrams.  To implement Java.
Programming TCP Clients. InetAddress Class An IP address identifies uniquely a host in the internet, which consists of 4 numbers (1 byte each one) in.
Network Programming CS3250. References Core Java, Vol. II, Chapter 3. Book examples are available from
Lecture 11 Java Socket Programming CPE 401 / 601 Computer Network Systems slides are modified from Dave Hollinger and Joonbok Lee.
Programming TCP Clients. InetAddress Class An IP address identifies uniquely a host in the internet, which consists of 4 numbers (1 byte each one) in.
Networking Support In Java Nelson Padua-Perez Bill Pugh Department of Computer Science University of Maryland, College Park.
Programming TCP Clients Version InetAddress Class An IP address identifies uniquely a host in the internet, which consists of 4 numbers (1 byte.
13-Jul-15 Sockets and URLs. 2 Sockets A socket is a low-level software device for connecting two computers together Sockets can also be used to connect.
Networking java.net package, which provides support for networking. Its creators have called Java “programming for the Internet.” Socket :- A network socket.
CEG3185 Tutorial 4 Prepared by Zhenxia Zhang Revised by Jiying Zhao (2015w)
TCP/IP Sockets in Java: Practical Guide for Programmers Kenneth L. Calvert Michael J. Donahoo.
Socket programming 1. getByName import java.net.*; public class GetHostName { public static void main (String args[]) { String host = "
NET0183 Networks and Communications Lecture 31 The Socket API 8/25/20091 NET0183 Networks and Communications by Dr Andy Brooks Lecture powerpoints from.
CSCE 515: Computer Network Programming Chin-Tser Huang University of South Carolina.
DBI Representation and Management of Data on the Internet.
1 CS: 4244 Internet Programming Sockets Programming Dr. Eli Tilevich January 29, 2007.
School of Engineering and Computer Science Victoria University of Wellington Copyright: Peter Andreae david streader, VUW Echo Networking COMP
UDP vs TCP UDP Low-level, connectionless No reliability guarantee TCP Connection-oriented Not as efficient as UDP.
Lecture 3: OSI Stack, Sockets, Protocols. EECE 411: Design of Distributed Software Applications Academic honesty is essential to the continued functioning.
Socket Programming in Java CS587x Lecture 4 Department of Computer Science Iowa State University.
Object Oriented Programming in Java Lecture 16. Networking in Java Concepts Technicalities in java.
Li Tak Sing COMPS311F. Case study: consumers and producers A fixed size buffer which can hold at most certain integers. A number of producers which generate.
Java Sockets Programming
Java Socket programming. Socket programming with TCP.
L 2 - 1 3( 1/ 20) : Java Network Programming. The Socket API The previous contents describe client-server interaction that application programs use when.
What is a Network? Computer network Computer network a set of computers using common protocols to communicate over connecting transmission media. a set.
Socket Programming Using JAVA Asma Shakil Semester 1, 2008/2009.
Networks Sockets and Streams. TCP/IP in action server ports …65535 lower port numbers ( ) are reserved port echo7 time13 ftp20 telnet23.
Sockets For Clients Instructors: Fu-Chiung Cheng ( 鄭福炯 ) Associate Professor Computer Science & Engineering Tatung University.
CSI 3125, Preliminaries, page 1 Networking. CSI 3125, Preliminaries, page 2 Inetaddress Class When establishing a connection across the Internet, addresses.
TCP/IP Protocol Stack IP Device Drivers TCPUDP Application Sockets (Gate to network) TCP: –Establish connection –Maintain connection during the communication.
Prepared by Dr. Jiying Zhao University of Ottawa Canada.
UNIT-6. Basics of Networking TCP/IP Sockets Simple Client Server program Multiple clients Sending file from Server to Client Parallel search server.
Java Programming II Java Network (I) Java Programming II.
MT311 Java Application Development and Programming Languages Li Tak Sing ( 李德成 )
1 CSCD 330 Network Programming Winter 2016 Some Material in these slides from J.F Kurose and K.W. Ross All material copyright Lecture 6 Application.
Spring/2002 Distributed Software Engineering C:\unocourses\4350\slides\DefiningThreads 1 Java API for distributed computing.
Agenda Socket Programming The OSI reference Model The OSI protocol stack Sockets Ports Java classes for sockets Input stream and.
Network Programming. These days almost all devices.
Network Programming Communication between processes Many approaches:
Java 13. Networking public class SumTest {
Java.net CS-328 Dick Steflik.
Object-Orientated Analysis, Design and Programming
Network Programming Introduction
NETWORK PROGRAMMING CNET 441
PRESENTED To: Sir Abid………. PRESENTED BY: Insharah khan………. SUBJECT:
Network Programming Introduction
Sockets and URLs 17-Sep-18.
Java Network Programming
Sockets and URLs 13-Nov-18.
Networking.
Sockets and URLs 3-Dec-18.
Java Socket Programming
Programming TCP Sockets
Programming TCP Clients
TCP/IP Sockets in Java: Practical Guide for Programmers
Presentation transcript:

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

1/22/20042 TCP Sockets in Java Use class Socket and class ServerSocket An instance of Socket represents one end of a TCP connection An instance of ServerSocket listens for client requests and creates a new Socket instance to handle each incoming connection Both client and server use Socket to send and receive messages

1/22/20043 Class Socket Constructors Socket(String host, int port) throws IOException Socket(InetAddress address, int port) throws IOException Socket(String host, int port, InetAddress localAddr, int localPort) throws IOException Socket(InetAddress address, int port, InetAddress localAddr, int localPort) throws IOException

1/22/20044 Class Socket Methods InputStream getInputStream() throws IOException OutputStream getOutputStream() throws IOException void close() throws IOException InetAddress getInetAddress() int getPort() InetAddress getLocalAddress() int getLocalPort()

1/22/20045 Class Socket Methods void setSoTimeout(int timeout) throws SocketException int getSoTimeout() throws SocketException void setTcpNoDelay(boolean on) throws SocketException boolean getTcpNoDelay() throws SocketException void setSoLinger(boolean on, int val) throws SocketException int getSoLinger() throws SocketException void setSendBufferSize(int size) throws SocketException int getSendBufferSize() throws SocketException void setReceiveBufferSize(int size) throws SocketException int getReceiveBufferSize() throws SocketException

1/22/20046 Class Socket Exceptions IOException SocketException BindException ConnectException NoRouteToHostException SecurityException

1/22/20047 A TCP Echo Client Example Construct a socket with specified server and port Communicate with the server using the socket’s I/O streams Display the echo from server Close the connection with server

1/22/20048 TCPEchoClient.java /* * TCP/IP Sockets in Java * Kenneth Calvert, Michael Donahoo * Morgan Kaufmann Publishers; ISBN * * de.html* * Copyright (c) 2002 Kenneth Calvert, Michael Donahoo ; * all rights reserved; see license.txt for details. */ import java.net.*; import java.io.*; public class TCPEchoClient { // public static void main (String args[]) … }

1/22/20049 Method main public static void main(String[] args) throws IOException { if ((args.length 3)) // Test for correct # of args throw new IllegalArgumentException("Parameter(s): [ ]"); String server = args[0]; // Server name or IP address // Convert input String to bytes using the default character encoding byte[] byteBuffer = args[1].getBytes(); int servPort = (args.length == 3) ? Integer.parseInt(args[2]) : 7; // Create socket that is connected to server on specified port Socket socket = new Socket(server, servPort); System.out.println("Connected to server...sending echo string"); InputStream in = socket.getInputStream(); OutputStream out = socket.getOutputStream(); out.write(byteBuffer); // Send the encoded string to the server …

1/22/ Method main (cont’d) // Receive the same string back from the server int totalBytesRcvd = 0; // Total bytes received so far int bytesRcvd; // Bytes received in last read while (totalBytesRcvd < byteBuffer.length) { if ((bytesRcvd = in.read(byteBuffer, totalBytesRcvd, byteBuffer.length - totalBytesRcvd)) == -1) throw new SocketException("Connection close prematurely"); totalBytesRcvd += bytesRcvd; } System.out.println("Received: " + new String(byteBuffer)); socket.close(); // Close the socket and its streams }

1/22/ Class ServerSocket Constructors ServerSocket(int port) throws IOException ServerSocket(int port, int backlog) throws IOException ServerSocket(int port, int backlog, InetAddress bindAddr) throws IOException

1/22/ Class ServerSocket Methods Socket accept() throws IOException void close() throws IOException InetAddress getInetAddress() int getLocalPort() Void setSoTimeout(int timeout) throws SocketException Int getSoTimeout() throws IOException

1/22/ Class ServerSocket Exceptions IOException SecurityException

1/22/ A TCP Echo Server Example Construct a server socket specifying local port to be listened to Repeatedly do the following Accept the next client connection request and create a socket for this connection Communicate with the client using the socket’s I/O streams Close the connection with client

1/22/ TCPEchoServer.java /* * TCP/IP Sockets in Java * Kenneth Calvert, Michael Donahoo * Morgan Kaufmann Publishers; ISBN * * de.html* * Copyright (c) 2002 Kenneth Calvert, Michael Donahoo ; * all rights reserved; see license.txt for details. */ import java.net.*; import java.io.*; public class TCPEchoServer { private static final int BUFSIZE = 32; // Size of receive buffer // public static void main (String args[]) … }

1/22/ Method main public static void main(String[] args) throws IOException { if (args.length != 1) // Test for correct # of args throw new IllegalArgumentException("Parameter(s): "); int servPort = Integer.parseInt(args[0]); // Create a server socket to accept client connection requests ServerSocket servSock = new ServerSocket(servPort); int recvMsgSize; // Size of received message byte[] byteBuffer = new byte[BUFSIZE]; // Receive buffer …

1/22/ Method main (cont’d) for (;;) { // Run forever, accepting and servicing connections Socket clntSock = servSock.accept(); // Get client connection System.out.println("Handling client at " + clntSock.getInetAddress().getHostAddress() + " on port " + clntSock.getPort()); InputStream in = clntSock.getInputStream(); OutputStream out = clntSock.getOutputStream(); // Receive until client closes connection, indicated by -1 return while ((recvMsgSize = in.read(byteBuffer)) != -1) out.write(byteBuffer, 0, recvMsgSize); clntSock.close(); // Close the socket. We are done with this client! } /* NOT REACHED */ }

1/22/ Next Class UDP sockets Non-blocking Read JNP Ch. 16, 20