Tcp Socket Java Program

Tcp Socket Java Program 4,5/5 5420votes

Tutorial on Socket Programming. Socket Preparation for Server Program Bind socket to the local address and port. Transmission Control Protocol. Below is the example source code from TCPIP Sockets in Java TM Practical Guide for Programmers by Kenneth L. Calvert and Michael J. Donahoo. Java Networking. The term network programming refers to writing programs that execute across multiple devices computers, in which the devices are all connected to each other using a network. The java. net package of the J2. SE APIs contains a collection of classes and interfaces that provide the low level communication details, allowing you to write programs that focus on solving the problem at hand. Example%3A+Java+client+%28TCP%29.jpg' alt='Tcp Socket Java Program' title='Tcp Socket Java Program' />Tcp Socket ExampleTcp Socket ProgrammingTcp In JavaTcp Socket Java ProgramThe java. TCP TCP stands for Transmission Control Protocol, which allows for reliable communication between two applications. TCP is typically used over the Internet Protocol, which is referred to as TCPIP. UDP UDP stands for User Datagram Protocol, a connection less protocol that allows for packets of data to be transmitted between applications. This chapter gives a good understanding on the following two subjects Socket Programming This is the most widely used concept in Networking and it has been explained in very detail. URL Processing This would be covered separately. Click here to learn about URL Processing in Java language. Socket Programming. Sockets provide the communication mechanism between two computers using TCP. A client program creates a socket on its end of the communication and attempts to connect that socket to a server. When the connection is made, the server creates a socket object on its end of the communication. The client and the server can now communicate by writing to and reading from the socket. The java. net. Socket class represents a socket, and the java. Server. Socket class provides a mechanism for the server program to listen for clients and establish connections with them. Tcp Socket Connection' title='Tcp Socket Connection' />The following steps occur when establishing a TCP connection between two computers using sockets The server instantiates a Server. Socket object, denoting which port number communication is to occur on. The server invokes the accept method of the Server. Socket class. This method waits until a client connects to the server on the given port. After the server is waiting, a client instantiates a Socket object, specifying the server name and the port number to connect to. The constructor of the Socket class attempts to connect the client to the specified server and the port number. If communication is established, the client now has a Socket object capable of communicating with the server. On the server side, the accept method returns a reference to a new socket on the server that is connected to the clients socket. Quarter Life Rapidshare. After the connections are established, communication can occur using IO streams. Each socket has both an Output. Stream and an Input. Stream. The clients Output. Stream is connected to the servers Input. Stream, and the clients Input. Stream is connected to the servers Output. Stream. TCP is a two way communication protocol, hence data can be sent across both streams at the same time. Following are the useful classes providing complete set of methods to implement sockets. Server. Socket Class Methods. The java. net. Server. Socket class is used by server applications to obtain a port and listen for client requests. The Server. Socket class has four constructors Sr. No. Method Description. Server. Socketint port throws IOException. Attempts to create a server socket bound to the specified port. An exception occurs if the port is already bound by another application. Server. Socketint port, int backlog throws IOException. Similar to the previous constructor, the backlog parameter specifies how many incoming clients to store in a wait queue. Server. Socketint port, int backlog, Inet. Address address throws IOException. Similar to the previous constructor, the Inet. Address parameter specifies the local IP address to bind to. The Inet. Address is used for servers that may have multiple IP addresses, allowing the server to specify which of its IP addresses to accept client requests on. Server. Socket throws IOException. Creates an unbound server socket. When using this constructor, use the bind method when you are ready to bind the server socket. If the Server. Socket constructor does not throw an exception, it means that your application has successfully bound to the specified port and is ready for client requests. Following are some of the common methods of the Server. Socket class Sr. No. Method Description. Mathematica 9 Serial Port on this page. Local. PortReturns the port that the server socket is listening on. This method is useful if you passed in 0 as the port number in a constructor and let the server find a port for you. Socket accept throws IOException. Waits for an incoming client. This method blocks until either a client connects to the server on the specified port or the socket times out, assuming that the time out value has been set using the set. So. Timeout method. Otherwise, this method blocks indefinitely. So. Timeoutint timeoutSets the time out value for how long the server socket waits for a client during the accept. Socket. Address host, int backlogBinds the socket to the specified server and port in the Socket. Address object. Use this method if you have instantiated the Server. Socket using the no argument constructor. When the Server. Socket invokes accept, the method does not return until a client connects. Rat 7 Mouse Software. After a client does connect, the Server. Socket creates a new Socket on an unspecified port and returns a reference to this new Socket. A TCP connection now exists between the client and the server, and communication can begin. Socket Class Methods. The java. net. Socket class represents the socket that both the client and the server use to communicate with each other. The client obtains a Socket object by instantiating one, whereas the server obtains a Socket object from the return value of the accept method. The Socket class has five constructors that a client uses to connect to a server Sr. No. Method Description. SocketString host, int port throws Unknown. Host. Exception, IOException. This method attempts to connect to the specified server at the specified port. If this constructor does not throw an exception, the connection is successful and the client is connected to the server. SocketInet. Address host, int port throws IOException. This method is identical to the previous constructor, except that the host is denoted by an Inet. Address object. 3public SocketString host, int port, Inet. Address local. Address, int local. Port throws IOException. Connects to the specified host and port, creating a socket on the local host at the specified address and port. SocketInet. Address host, int port, Inet. Address local. Address, int local. Port throws IOException. This method is identical to the previous constructor, except that the host is denoted by an Inet. Address object instead of a String. SocketCreates an unconnected socket. Use the connect method to connect this socket to a server. When the Socket constructor returns, it does not simply instantiate a Socket object but it actually attempts to connect to the specified server and port. Some methods of interest in the Socket class are listed here. Notice that both the client and the server have a Socket object, so these methods can be invoked by both the client and the server. Sr. No. Method Description. Socket. Address host, int timeout throws IOException. This method connects the socket to the specified host. This method is needed only when you instantiate the Socket using the no argument constructor. Inet. Address get. TCPIP Sockets in Java Practical Guide for Programmers. Below is the example source code from TCPIP Sockets in Java. TM. Practical Guide for Programmers by Kenneth. L. Calvert and Michael J. Donahoo. This book can be ordered at your favorite local bookstore. Official. Book Website. Disclaimer The purpose of this book is to provide general. The authors have included sample code that is intended for the. API. Neither the. The authors. and the Publisher DISCLAIM ALL EXPRESS AND IMPLIED WARRANTIES. Your use or reliance upon any sample code or other. No one should use.