Normally, a server runs on a specific computer & has a socket that is bound to a specific port number. The server just waits, listening to the socket for a client to lớn make a connection request.

Bạn đang xem: Socket

On the client-side: The client knows the hostname of the machine on which the vps is running and the port number on which the vps is listening. To lớn make a connection request, the client tries to lớn rendezvous with the server on the server's machine & port. The client also needs khổng lồ identify itself to lớn the hệ thống so it binds khổng lồ a local port number that it will use during this connection. This is usually assigned by the system.


If everything goes well, the vps accepts the connection. Upon acceptance, the server gets a new socket bound to lớn the same local port and also has its remote endpoint set lớn the address and port of the client. It needs a new socket so that it can continue to listen lớn the original socket for connection requests while tending to the needs of the connected client.


On the client side, if the connection is accepted, a socket is successfully created & the client can use the socket to lớn communicate with the server.

The client & server can now communicate by writing to lớn or reading from their sockets.

Xem thêm: Metadata Là Gì Và Metadata Bao Gồm Những Thông Tin Gì? Tầm Quan Trọng Của Metadata


A socket is one endpoint of a two-way communication link between two programs running on the network. A socket is bound lớn a port number so that the TCP layer can identify the application that data is destined lớn be sent to.

An endpoint is a combination of an IP address & a port number. Every TCP connection can be uniquely identified by its two endpoints. That way you can have multiple connections between your host & the server.

The package in the Java platform provides a class, Socket, that implements one side of a two-way connection between your Java program và another program on the network. The Socket class sits on đứng đầu of a platform-dependent implementation, hiding the details of any particular system from your Java program. By using the class instead of relying on native code, your Java programs can communicate over the network in a platform-independent fashion.

Additionally, includes the ServerSocket class, which implements a socket that servers can use to listen for và accept connections to clients. This lesson shows you how khổng lồ use the Socket và ServerSocket classes.

If you are trying to connect to the Web, the URL class and related classes (URLConnection, URLEncoder) are probably more appropriate than the socket classes. In fact, URLs are a relatively high-level connection lớn the Web & use sockets as part of the underlying implementation. See Working with URLs for information about connecting to lớn the website via URLs.