JavaSE-TCP network programming (1), javase Network Programming
Connect the client to the server:
- Use ServerSocket to create a TCP Server
ServerSocket: This class implements server sockets. Server socket requests are transmitted over the network. Some operations are performed based on the request, and the results may be returned to the requester.
See: http://download.java.net/jdk/jdk-api-localizations/jdk-api-zh-cn/publish/1.6.0/html/zh_CN/api/index.html for details
ServerSocket class construction method:
ServerSocket() Creates a non-bound server socket. |
ServerSocket(int port) Create a server socket bound to a specific port. |
ServerSocket(int port, int backlog) Use the specified backlog to create a server socket and bind it to the specified local port number. |
ServerSocket(int port, int backlog, InetAddress bindAddr) Use the specified port, listener backlog, and local IP address to create the server. |
Common ServerSocket class methods
Socket |
accept() Listen and be connected to this socket. |
void |
bind(SocketAddress endpoint, int backlog) SetServerSocket Bind to a specific address (IP address and port number ). |
void |
close() Disable this socket. |
InetAddress |
getInetAddress() Returns the local address of the server socket. |
boolean |
isBound() Returns the binding status of ServerSocket. |
boolean |
isClosed() The ServerSocket is closed. |
Brief code:
ServerSocket server = new ServerSocket (valid port number );
Server client = server. accept (); // when the client connection request is not received, accept is blocked and continues to run after receiving the request.
If (client! = Null ){
System. out. println ("a client is connected ");
}
- Use Socket to create a TCP client
Socket class: This class implements client Sockets ("Sockets"). sockets are the endpoints for communication between two machines.
Common constructor methods:
|
Socket() Use SocketImpl of the default system type to create an unconnected socket |
|
Socket(InetAddress address, int port) Create a stream socket and connect it to the specified port number of the specified IP address. |
|
Socket(InetAddress address, int port, InetAddress localAddr, int localPort) Create a socket and connect it to the specified remote port on the specified remote address. |
protected |
Socket(SocketImpl impl) Use SocketImpl specified by the user to create an unconnected Socket. |
|
Socket(String host, int port) Create a stream socket and connect it to the specified port number on the specified host. |
|
Socket(String host, int port, InetAddress localAddr, int localPort) Create a socket and connect it to the specified remote port on the specified remote host. |
Common Methods:
void |
bind(SocketAddress bindpoint) Bind the socket to a local address. |
void |
close() Disable this socket. |
void |
connect(SocketAddress endpoint) Connect the socket to the server. |
Brief code:
Socket client = new Socket ("127.0.0.0", 9999); server IP address and port number.
Query the IP address and port number of a computer:
Open a command prompt and enter:
Ipconfig/all (windows ip configuration window operating system ip configuration) view the current IP address and computer network configuration; display all information about the current computer ip address, including the ip address, Nic (mac) address.
Netstat-an: view all current connection ports. netstat (a program that accesses the network and related information in the kernel) displays network connection, route table, and network interface information, this allows you to know which network connections are currently in operation.
Package network programming; import java. io. IOException; import java.net. serverSocket; import java.net. socket; public class Server {public static void main (String [] args) {try {ServerSocket server = new ServerSocket (9657); Socket client = server. accept (); if (client! = Null) {System. out. println ("a client is connected"); server. close () ;}} catch (IOException e) {// TODO Auto-generated catch block e. printStackTrace ();}}}
Package network programming; import java. io. IOException; import java.net. socket; import java.net. unknownHostException; public class Client {public static void main (String [] args) {try {Socket client = new Socket ("127.0.0.1", 9657); client. close ();} catch (UnknownHostException e) {// TODO Auto-generated catch block e. printStackTrace ();} catch (IOException e) {// TODO Auto-generated catch block e. printStackTrace ();}}}
Output: a client is connected.
This section of the server code has a problem. After the client establishes a connection with the server, the service is automatically closed and cannot continue to accept requests from multiple clients. A while loop can be added to enable the server to accept a client request and return to the acceptance status of accept.