tcp_ip
socket吧
全部回复
仅看楼主
level 1
domzhao2 楼主
客户端
import java.net.*;    // for Socket
import java.io.*;     // for IOException and Input/OutputStream
public class TCPEchoClient {
    public static void main(String[] args) throws IOException {
      if ((args.length < 2) || (args.length > 3))    // Test for correct # of args
        throw new IllegalArgumentException("Parameter(s): <Server> <Word> [<Port>]");
      String server = args[0];         // Server name or IP address
      // Convert input String to bytes using the default character encoding
      byte[] byteBuffer = args[1].getBytes();
      int servPort = (args.length == 3) ? Integer.parseInt(args[2]) : 7;
      // Create socket that is connected to server on specified port
      Socket socket = new Socket(server, servPort);
      System.out.println("Connected to server...sending echo string");
      InputStream in = socket.getInputStream();
      OutputStream out = socket.getOutputStream();
      out.write(byteBuffer);    // Send the encoded string to the server
      // Receive the same string back from the server
      int totalBytesRcvd = 0;    // Total bytes received so far
      int bytesRcvd;             // Bytes received in last read
      while (totalBytesRcvd < byteBuffer.length) {
        if ((bytesRcvd = in.read(byteBuffer, totalBytesRcvd,  
                          byteBuffer.length - totalBytesRcvd)) == -1)
          throw new SocketException("Connection close prematurely");
        totalBytesRcvd += bytesRcvd;
      }
      System.out.println("Received: " + new String(byteBuffer));
      socket.close();    // Close the socket and its streams
    }
}
服务端
import java.net.*;    // for Socket, ServerSocket, and InetAddress
import java.io.*;     // for IOException and Input/OutputStream
public class TCPEchoServer {
    private static final int BUFSIZE = 32;     // Size of receive buffer

2011年01月22日 14点01分 1
level 1
domzhao2 楼主

    public static void main(String[] args) throws IOException {
      if (args.length != 1)    // Test for correct # of args
        throw new IllegalArgumentException("Parameter(s): <Port>");
      int servPort = Integer.parseInt(args[0]);
      // Create a server socket to accept client connection requests
      ServerSocket servSock = new ServerSocket(servPort);
      int recvMsgSize;     // Size of received message
      byte[] byteBuffer = new byte[BUFSIZE];    // Receive buffer
      for (;;) { // Run forever, accepting and servicing connections
        Socket clntSock = servSock.accept();       // Get client connection
        System.out.println("Handling client at " +
          clntSock.getInetAddress().getHostAddress() + " on port " +
               clntSock.getPort());
        InputStream in = clntSock.getInputStream();
        OutputStream out = clntSock.getOutputStream();
        // Receive until client closes connection, indicated by -1 return
        while ((recvMsgSize = in.read(byteBuffer)) != -1)
          out.write(byteBuffer, 0, recvMsgSize);
        clntSock.close();    // Close the socket.    We are done with this client!
      }
      /* NOT REACHED */
    }
}
打电话的时候,通常是被呼叫的人说"你好"。要实现这个机制,我们的客户端和服务器端例子需要作什么改变?
2011年01月22日 14点01分 2
1