⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 client.java

📁 client server using socket programing with best optimized way to do .. and how to do guide is als
💻 JAVA
字号:
import java.net.*;
import java.io.*;

public class Client{

	public static void main(String[] ar) {
		int serverPort = 6666; // make sure you give the port number on which the server is listening.
		String address = "127.0.0.1"; // this is the IP address of the server program's computer. // the address given here means "the same computer as the client".


		try {
			InetAddress ipAddress = InetAddress.getByName(address); // create an object that represents the above IP address.
	
			System.out.println("Any of you heard of a socket with IP address " + address + " and port " + serverPort + "?");
 	
 			Socket socket = new Socket(ipAddress, serverPort); // create a socket with the server's IP address and server's port.

			System.out.println("Yes! I just got hold of the program.");
 			
 			// Get the input and output streams of the socket, so that you can receive and send data to the client.
			InputStream sin = socket.getInputStream();
			OutputStream sout = socket.getOutputStream();

 			// Just converting them to different streams, so that string handling becomes easier.
			DataInputStream in = new DataInputStream(sin);
			DataOutputStream out = new DataOutputStream(sout);
 	
 			// Create a stream to read from the keyboard.
			BufferedReader keyboard = new BufferedReader(new InputStreamReader(System.in));
 	
 			String line = null;
			System.out.println("Type in something and press enter. Will send it to the server and tell ya what it thinks.");
			System.out.println();
 			
 			while(true) {
				line = keyboard.readLine(); // wait for the user to type in something and press enter.
 	
 				System.out.println("Sending this line to the server...");
	
				out.writeUTF(line); // send the above line to the server.
				out.flush(); // flush the stream to ensure that the data reaches the other end.
				line = in.readUTF(); // wait for the server to send a line of text.
 	
 				System.out.println("The server was very polite. It sent me this : " + line);
				System.out.println("Looks like the server is pleased with us. Go ahead and enter more lines.");
				System.out.println();
			}
		} catch(Exception x) {
			x.printStackTrace();
		}
	}
}

⌨️ 快捷键说明

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