Anda di halaman 1dari 17

Socket Programming in Java

-First Step of Network Programming-

Basics of Networking
What Is Socket???

The Java Socket API

Types of Sockets Socket Programming with TCP Sockets for server and client Example

Basics of Networking:
A protocol is a set of rules that determine how things communicate with each other. The software which manages Internet communication follows a suite of protocols called TCP/IP. The Internet Protocol (IP) determines the format of the information as it is transferred. The Transmission Control Protocol (TCP) dictates how messages are reassembled and handles lost information.

What Is a Socket???
Sockets are the basic mechanism for inter-process communication provided by the operating system. A socket is essentially a defined endpoint for communication between two processes. It provides a full duplex channel to two different parties involved in communication. There are two separate data streams, one going in and one going out.

The Java Socket API:

The Java Socket API is the core Java interface to network programming. As such, all of the core socket classes are found in the package. Java implements the two types of sockets:
TCP sockets , which communicate using the Transmission Control Protocol, and UDP sockets, which communicate via the Universal Datagram Protocol.

In Package Implements client sockets (also called just sockets). An endpoint for communication between two machines. Constructor and Methods Socket(String host, int port): Creates a stream socket and connects it to the specified port number on the named host. InputStream getInputStream() OutputStream getOutputStream() close()

In Package Implements server sockets. Waits for requests to come in over the network. Performs some operation based on the request. Constructor and Methods ServerSocket(int port) Socket Accept(): Listens for a connection to be made to this socket and accepts it. This method blocks until a connection is made.

Socket functional calls

socket (): Create a socket bind(): bind a socket to a local IP address and port # listen(): passively waiting for connections connect(): initiating connection to another socket accept(): accept a new connection Write(): write data to a socket Read(): read data from a socket sendto(): send a datagram to another UDP socket recvfrom(): read a datagram from a UDP socket close(): close a socket (tear down the connection)

Types of Socket:
User Datagram Protocol (UDP) Sockets:
Sockets using UDP provide a datagram service. They receive and send discrete packets of data. UDP is a connectionless protocol, meaning that there is no connection setup time as there is in TCP. UDP is unreliablepackets are not guaranteed to be sent or received in the right order. UDP is mainly used for applications such as multimedia streaming and online gaming.

Transmission Control Protocol (TCP) Sockets:

Sockets using TCP provide a reliable byte-stream service. TCP guarantees delivery of all packets sent and the reception of them in the correct order. TCP is a connection-oriented protocol, which allows it to provide the byte-stream service. TCP is best suited for applications that cannot allow data transmitted to be lost, such as for file transfer, web browsing, or Telnet.

Socket Programming with TCP:

How to Open a Client Socket?

Socket MyClient; MyClient = new Socket("Machine name", PortNumber);
Machine name is the machine you are trying to open a connection to(ex: ip address or workstation name), PortNumber is the port (a number) on which the server you are trying to connect to is running.

How to handle the port numbers?

When selecting a port number, you should note that port numbers between 0 and 1,023 are reserved for privileged users These port numbers are reserved for standard services, such as email, FTP, and HTTP. When selecting a port number for your server, select one that is greater than 1,023!

How to Open a Server Socket?

ServerSocket MyService; try { MyServerice = new ServerSocket(PortNumber); } catch (IOException e) { System.out.println(e); }

Sockets for server and client

Welcoming socket Welcomes some initial contact from a client. Connection socket Is created at initial contact of client. New socket that is dedicated to the particular client.

Client socket Initiate a TCP connection to the server by creating a socket object. (Three-way handshake) Specify the address of the server process, namely, the IP address of the server and the port number of the process.

Sockets for server and client

Sockets for server and client

Server (running on hostid)
create socket, port=x, for incoming request: welcomeSocket = ServerSocket() wait for incoming connection request connection connectionSocket = welcomeSocket.accept() read request from connectionSocket write reply to connectionSocket close connectionSocket




create socket, connect to hostid, port=x clientSocket = Socket() send request using clientSocket

read reply from clientSocket close clientSocket

Thank You