Sockets For Clients Instructors: Fu-Chiung Cheng ( 鄭福炯 ) Associate Professor Computer Science & Engineering Tatung University.

Slides:



Advertisements
Similar presentations
CSCI Java Networking1 Java Net Classes ClassDescription DatagramPacketThis class represents a datagram packet. DatagramSocketThis class represents.
Advertisements

Socket Programming ENTERPRISE JAVA. 2 Content  Sockets  Streams  Threads  Readings.
Sockets for Clients Socket Basics  Connect to a remote machine  Send data  Receive data  Close a connection  Bind to a port 
Sockets For Servers Instructors: Fu-Chiung Cheng ( 鄭福炯 ) Associate Professor Computer Science & Engineering Tatung University.
UDP Datagrams and Sockets Instructors: Fu-Chiung Cheng ( 鄭福炯 ) Associate Professor Computer Science & Engineering Tatung University.
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.
Basic Socket Programming with Java. What is a socket?  Generally refers to a stream connecting processes running in different address spaces (across.
Socket Programming.
User Datagram Protocol. Introduction UDP is a connectionless transport protocol, i.e. it doesn't guarantee either packet delivery or that packets arrive.
Prepared By E. Musa Alyaman1 User Datagram Protocol (UDP) Chapter 5.
1 Java Networking – Part I CS , Spring 2008/9.
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.
Networking Support In Java 2 Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Java Socket Support Presentation by: Lijun Yuan Course Number: cs616.
Internet Programming In Java. References Java.sun.com Java552 Many of the programs shown.
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)
Socket Programming -What is it ? -Why bother ?. Basic Interface for programming networks at transport level It is communication end point Used for inter.
Multicast Sockets What is a multicast socket?
NET0183 Networks and Communications Lecture 31 The Socket API 8/25/20091 NET0183 Networks and Communications by Dr Andy Brooks Lecture powerpoints from.
 Socket  The combination of an IP address and a port number. (RFC 793 original TCP specification)  The name of the Berkeley-derived application programming.
DBI Representation and Management of Data on the Internet.
1 CSCD 330 Network Programming Winter 2015 Some Material in these slides from J.F Kurose and K.W. Ross All material copyright Lecture 6 Application.
More on Socket API. How to Place Timeouts on Sockets (1)  Using SIGALRM signal Connection timeout 기간의 축소 Response timeout advio/dgclitimeo3.clib/connect_timeo.c.
RGEC MEERUT(IWT CS703) 1 Java Networking RGEC Meerut.
Socket Programming in Java CS587x Lecture 4 Department of Computer Science Iowa State University.
1 A TCP/IP Application Programming Perspective Chris Greenhalgh G53ACC.
Object Oriented Programming in Java Lecture 16. Networking in Java Concepts Technicalities in java.
Lecture 9 Network programming. Manipulating URLs URL is an acronym for Uniform Resource Locator and is a reference (an address) to a resource on the Internet.
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.
1 Chapter 28 Networking. 2 Objectives F To comprehend socket-based communication in Java (§28.2). F To understand client/server computing (§28.2). F To.
Java Sockets Programming
L 2 - 1 3( 1/ 20) : Java Network Programming. The Socket API The previous contents describe client-server interaction that application programs use when.
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.
By Vivek Dimri. Basic Concepts on Networking IP Address – Protocol – Ports – The Client/Server Paradigm – Sockets The Java Networking Package – The InetAddress.
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.
1 Lecture 9: Network programming. 2 Manipulating URLs URL is an acronym for Uniform Resource Locator and is a reference (an address) to a resource on.
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.
Agenda Socket Programming The OSI reference Model The OSI protocol stack Sockets Ports Java classes for sockets Input stream and.
UDP Programming. Khoa CNTT – ĐH Nông Lâm TP. HCM 01/2007 2/86 Overview.
Client/Server Design Issues Based on Java Network Programming and Distributed Computing, Chapter 6 Also Online Java Tutorial, Sun.
CSCE 515: Computer Network Programming Chin-Tser Huang University of South Carolina.
Network Programming. These days almost all devices.
School of Engineering and Computer Science Victoria University of Wellington Copyright: Peter Andreae david streader, VUW Echo Networking COMP
Network Programming Communication between processes Many approaches:
Java.net CS-328 Dick Steflik.
Object-Orientated Analysis, Design and Programming
Socket Option.
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
Networking.
Sockets and URLs 3-Dec-18.
Programming TCP Sockets
Programming TCP Clients
Presentation transcript:

Sockets For Clients Instructors: Fu-Chiung Cheng ( 鄭福炯 ) Associate Professor Computer Science & Engineering Tatung University

Contents The Socket class Telnet and Telnet protocols Socket information Socket Options InetAddress class

Socket A socket is a connection between two hosts Socket: client and server socket – Socket class (client) and ServerSocket (server) Client socket operations: – connect to a remote machine (a.k.a. server) – send data – receive data – close a connection

Server Socket Server Socket operations: – connect to a remote machine (a.k.a. server) – send data – receive data – close a connection – bind to a port (register the port to host) – listen for incoming data – accept connections from remote machines on the bound part

Telnet and Telnet Protocols Telnet command –%telnet localhost 25 or telnet Port 25 provides SMTP (Simple Mail Transfer Protocol) service. Protocol: Set of rules and conventions used by communicating participants check page 303 for the SMTP protocol –Type: HELO command –Reponse: 250 gamma. Hello alpha.cse.ttu.edu.tw [ ], pleased to meet you

Telnet and Telnet Protocols –Type HELP … 214-Commands: 214- HELO MAIL RCPT DATA RSET 214- NOOP QUIT HELP VRFY EXPN 214-For more info use "HELP ". 214-smtp 214-To report bugs in the implementation contact Sun Microsystems 214-Technical Support. 214-For local information contact postmaster at this site. 214 End of HELP info

Socket Class Client-side TCP operations URL, URLConnection, Applet and JEditorPane use Socket for client communication. Constructors: (page ) –Socket public Socket(InetAddress address,int port) –public Socket(String host, int port, InetAddress localAddr, int localPort) – public Socket(InetAddress address, int port, InetAddress localAddr, int localPort)

Socket Class Socket theSocket = new Socket(" ", 13); –Connect to host " “ and –use port 13 service InetAddress fddi = new InetAddress(“fddisunsie.oit.unc.edu”); Socket theSocket = new Socket( 80, fddi, 0) –Use fddi network interface –Local port 1024~65535

Socket Information public InetAddress getInetAddress() –Returns the address to which the socket is connected. –Returns: the remote IP address to which this socket is connected. public InetAddress getLocalAddress() –Gets the local address to which the socket is bound. –Returns:the local address to which the socket is bound.

Socket Information public int getPort() –Returns the remote port to which this socket is connected. –Returns:the remote port number to which this socket is connected public int getLocalPort() –Returns the local port to which this socket is bound. –Returns:the local port number to which this socket is connected.

Socket Information Example 10-3 (SocketInfo) on page 313 Check and www.ttu.edu.tw Outputs: –Connected to / on port 80 from port 1118 of JAVA.ttu.edu.tw/ –Connected to on port 80 from port 1122 of JAVA.ttu.edu.tw/

Socket Information public InputStream getInputStream() –Get the input stream for this socket. –Returns:an input stream for reading bytes from this socket. public OutputStream getOutputStream() –Get the output stream for this socket. –Returns:an output stream for writing bytes to this socket.

Socket Information Example 10-4 (Daytime Protocol Client) on page 314 Daytime protocol: Connect to and port 13 Use getInputStream to read the daytime Outputs: –It is Tue Mar 27 09:45: at

Socket Information Example 10-4 (Daytime Protocol Client) on page 314 Daytime protocol: Connect to and port 13 Use getInputStream to read the daytime Outputs: –It is Tue Mar 27 09:45: at More complicate sample: Time protocol on page 317

Socket Information Example 10-6 (Echo Protocol Client) on page 321 Echo protocol: Connect to and port 7 You may try telnet Use getInputStream to read the echo string from server Use getOutputStream to write string to server

Closing the Socket It is very good practice to close sockets. System may hit max number of sockets before GC kicks in. (Especially for Browser app.) public void close() –Closes this socket. public void shutdownInput() –Places the input stream for this socket at "end of stream". –Any data sent to the input stream side of the socket is acknowledged and then silently discarded. –If you read from a socket input stream after invoking shutdownInput() on the socket, the stream will return EOF.

Closing the Socket public void shutdownOutput() –Disables the output stream for this socket. –For a TCP socket, any previously written data will be sent followed by TCP's normal connection termination sequence. –If you write to a socket output stream after invoking shutdownOutput() on the socket, the stream will throw an IOException.

Closing the Socket Example: PortScan on page 323 Code sample: close a socket finally { try { if (connection != null) connection.close(); } catch (IOException e) {} }

Socket Options public void setTcpNoDelay(boolean on) –Enable/disable TCP_NODELAY (disable/enable Nagle's algorithm). –Parameters:on - true to enable TCP_NODELAY, false to disable public boolean getTcpNoDelay() –Tests if TCP_NODELAY is enabled. –Returns:a boolean indicating whether or not TCP_NODELAY is enabled.

Socket Options Datagrams not yet been sent but Socket is closed public void setSoLinger(boolean on, int linger) –Enable/disable SO_LINGER with the specified linger time in seconds. –The maximum timeout value is platform specific. –The setting only affects socket close. public int getSoLinger() –Returns setting for SO_LINGER. –-1 returns implies that the option is disabled. Example: –If (s.getSoLinger() == -1) s.setSoLinger(true, 240);

Socket Options public void setSoTimeout(int timeout) –Enable/disable SO_TIMEOUT with the specified timeout, in milliseconds. –With this option set to a non-zero timeout, a read() call on the InputStream associated with this Socket will block for only this amount of time. –If the timeout expires, a java.io.InterruptedIOException is raised, though the Socket is still valid. –The option must be enabled prior to entering the blocking operation to have effect. –The timeout must be > 0. –A timeout of zero is interpreted as an infinite timeout.

Socket Options public int getSoTimeout() –Returns setting for SO_TIMEOUT. –0 returns implies that the option is disabled (i.e., timeout of infinity). Example: –If (s.getSoTimeout() == 0) s.setSoTimeout(180000);

Socket Options public void setSendBufferSize(int size) –Sets the SO_SNDBUF option to the specified value for this Socket. –The SO_SNDBUF option is used by the platform's networking code as a hint for the size to set the underlying network I/O buffers. –Increasing buffer size can increase the performance of network I/O for high-volume connection, while decreasing it can help reduce the backlog of incoming data. –Because SO_SNDBUF is a hint, applications that want to verify what size the buffers were set to should call getSendBufferSize()

Socket Options public int getSendBufferSize() –Get value of the SO_SNDBUF option for this Socket, that is the buffer size used by the platform for output on this Socket

Socket Options public void setReceiveBufferSize(int size) –Sets the SO_RCVBUF option to the specified value for this Socket. –The SO_RCVBUF option is used by the platform's networking code as a hint for the size to set the underlying network I/O buffers. –Increasing buffer size can increase the performance of network I/O for high-volume connection, while decreasing it can help reduce the backlog of incoming data. –Because SO_RCVBUF is a hint, applications that want to verify what size the buffers were set to should call getReceiveBufferSize()

Socket Options public int getReceiveBufferSize() –Gets the value of the SO_RCVBUF option for this Socket, that is the buffer size used by the platform for input on this Socket

Socket Options If SO_KEEPALIVE is turned on, then the client will occasionally send a data packet over an idle connection (once every two hours) just to make sure the server hasn’t crashed. public void setKeepAlive(boolean on) –Enable/disable SO_KEEPALIVE. –Parameters:on - whether or not to have socket keep alive turned on. public boolean getKeepAlive() –Tests if SO_KEEPALIVE is enabled.

InetAddress Class public static InetAddress getByName(String host) –Determines the IP address of a host, given the host's name. –The host name can either be a machine name, such as "java.sun.com", or a string representing its IP address, such as " ". public static InetAddress[] getAllByName(String host) –Determines all the IP addresses of a host, given the host's name. public static InetAddress getLocalHost() –Returns the local host.

InetAddress Class public InetAddress(String host) –Create a InetAddress of a host, given the host's name. public public byte[] getAddress() –Returns the raw IP address of this InetAddress object. –The result is in network byte order: the highest order byte of the address is in getAddress()[0]. public String getHostAddress() –Returns the IP address string "%d.%d.%d.%d".

InetAddress Class public String getHostName() –Gets the host name for this IP address. public boolean isMulticastAddress() –Utility routine to check if the InetAddress is an IP multicast address. –IP multicast address is a Class D address i.e first four bits of the address are 1110.