Java socket programming (IV) duplicate and concurrent servers

Duplicate and concurrent server this application is treated as a duplicate server Because it only accepts another connection after processing one process More complex servers are concurrent It allocates a thread for each request instead of processing one by one So it looks like it's processing multiple requests at the same time All commercial servers are concurrent servers Unlike connection - oriented classes, the client - side and server - side classes of Java datagrams are superficially the same The following program establishes a datagram socket for clients and server providers: datagram socket ServerSocket = new datagram socket (4545); DatagramSocket clientSocket = new DatagramSocket(); The server uses parameter 4545 to specify the port number. Since the client will call the server, the client can use the available port If the second parameter is omitted, the program will ask the operating system to assign an available port The client can request a specified port, but if other applications are bound to this port, the request will fail If your intention is not to be a server, you'd better not specify a port Since the stream cannot be obtained by conversation, how can we talk to a datagram socket The answer lies in the datagram class Receiving datagram datagram packet class is a class used to receive and send data through datagram socket class The packet class includes connection information and data As mentioned above, datagrams are their own independent transmission units The datagram packet class compresses these units The following program shows that a datagram socket is used to receive data: datagram packet packet = new datagram packet (new byte [512], 512); clientSocket. receive(packet); clientSocket. receive(packet); The packet builder needs to know where to put the obtained data A 512 byte cache is created and used as the second parameter of the builder Every second builder parameter is the size of the cache Like the accept () method of the ServerSocket class, the receive () method will block until data is available Sending datagram sending datagram is very simple. All you need is an address The address is created by the InetAddress class This class does not have a common builder, but it has several static methods that can be used to create instances of this class The following list lists the methods to create an instance of the InetAddress class: public InetAddress creation methods InetAddress getbyname (string host); InetAddress[] getAllByName(String host); InetAddress getLocalHost(); It is very useful to get the address of the local host. Only the first two methods are used to send packets Getbyname() and getallbyname() require the address of the destination host The first method simply returns the first qualified thing The second method is necessary because a computer may have multiple addresses In this case, this computer is called multi homed All established methods are marked as static They must be called like this: InetAddress ADDR1 = InetAddress getByName("merlin"); InetAddress addr2[] = InetAddress. getAllByName("merlin"); InetAddress addr3 = InetAddress. getLocalHost();

All these calls can throw an unknownhostexception violation If a computer is not connected to the DNS server, or the host is not found, this violation will be thrown If a computer does not have an active TCP / IP configuration, getlocalhost() also fails and throws a violation Once an address is determined, the datagram can be sent out The following program transfers a string to the destination socket: String tosend = "this is the data to send!"); byte[] sendbuf = new byte[ toSend.length() ]; toSend. getBytes( 0,toSend.length(),sendbuf,0 ); DatagramPacket sendPacket = new DatagramPacket( sendbuf,sendbuf.length, addr,port); clientSocket. send( sendPacket ); First, the string must be converted to a byte array Then, a new datagram packet instance must be created Notice the last two parameters of the builder Because a packet is to be sent, the address and port must be given An applet may know the address of its server, but how does the server know the address of its client When any packet is received, the returned address and port will be extracted and obtained through getaddress () and getport () methods This is how a server responds to a client's packet: datagram packet sendpacket = new datagram packet (sendbuf, recvpacket. Getaddress(), recvpacket getPort() ); serverSocket. send( sendPacket ); Unlike connection oriented operation, datagram server is actually simpler than datagram client: datagram server the basic steps of a datagram server: 1 Create a datagram socket on a specified port 2. Use the receive method to wait for incoming packets 3. Respond to received packets with a specific protocol 4. Return to step 2 or continue with step 2 5. Close datagram socket Listing 9.3 demonstrates a simple datagram response server It will respond to the package it receives Table 9.3 A simple datagram response server import Java io.*; import java. net.*; public class SimpleDatagramServer { public static void main(String[] args) { DatagramSocket socket = null; DatagramPacket recvPacket,sendPacket; try { socket = new DatagramSocket(4545); while (socket != null) { recvPacket= new DatagramPacket(new byte[512],512); socket.receive(recvPacket); sendPacket = new DatagramPacket( recvPacket.getData() ,recvPacket. getLength(),recvPacket. getPort() ); socket. send( sendPacket ); } } catch (SocketException se) { System.out.println("Error in SimpleDatagramServer: " + se); } catch (IOException ioe) { System.out.println("Error in SimpleDatagramServer: " + ioe);

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>