tcpclient.java

来自「client server socket programs to learn s」· Java 代码 · 共 55 行

JAVA
55
字号
import java.util.*;
import java.io.*;
import java.net.*;

public class TCPClient {

	public static void main (String args[]) throws Exception {
		// throws Exception here because don't want to deal
		// with errors in the rest of the code for simplicity.
		// (note: NOT a good practice!)

		// Connect to the server process running at host
		//  and port given at command line.
		InetAddress address = InetAddress.getByName(args[0]);
		int port = Integer.parseInt(args[1]);
		Socket s = new Socket(address, port);

	    // The next 2 lines create a output stream we can
		// write to.  (To write TO SERVER)
		OutputStream os= s.getOutputStream();
		DataOutputStream serverWriter = new DataOutputStream(os);

		// The next 2 lines create a buffer reader that
		// reads from the standard input. (to read stream FROM SERVER)
		System.out.println("Enter a Sentence to send to the server. To end this session, enter a blank line ");
		InputStreamReader isrServer = new InputStreamReader(s.getInputStream());
		BufferedReader serverReader = new BufferedReader(isrServer);


        //create buffer reader to read input from user. Read the user input to string 'sentence'
        BufferedReader inFromUser = new BufferedReader(new InputStreamReader(System.in));
        String sentence;
        sentence = inFromUser.readLine();

        // keep repeating until an empty line is read.
		while (sentence.compareTo("") != 0) {
           // Send a user input to server
           serverWriter.writeBytes(sentence +"\n");

		   // Server should convert to upper case and reply.
		   // Read server's reply below and output to screen.
           String response = serverReader.readLine();
		   System.out.println(response);
           //read user input again
           sentence = inFromUser.readLine();
           }


		// Send an empty line to server to end communication.
		serverWriter.writeBytes("\n");

		s.close();
	}
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?