Java Socket通信 客户端服务器端基本代码

【服务器端】

import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;

public class TestServer {

	public static void main(String[] args) {
		try {
			System.out.println("启动服务端");
			ServerSocket sv = new ServerSocket(2233);
			//等待用户链接
			Socket socket = sv.accept();
			System.out.println("有用户链接!");
			while (true) {
				//1
				InputStream is = socket.getInputStream();
				//2
				byte[] data = new byte[128];
				is.read(data,0,data.length);
				//3 按照格式转成字符串输出 
				String msg = new String(data, "UTF-8");
				//收到的内容
				System.out.println("收到的内容:"+ msg);
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}

【客户机端】

import java.io.IOException;
import java.io.OutputStream;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Scanner;

public class TestClient {

	public static void main(String[] args) {
		try {
			Socket socket = new Socket("192.169.191.4", 2244);
//			Socket socket = new Socket("localhost", 2233);
			System.out.println("链接服务器成功!");
			while (true) {
				System.out.println("请输入内容:");
				//1
				Scanner input = new Scanner(System.in);
				String msg = input.next();
				//2
				OutputStream os = socket.getOutputStream();
				//3
				byte[] data = msg.getBytes("UTF-8");
				os.write(data);
				os.flush();
			}
		} catch (UnknownHostException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

}


  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java中可以通过Socket实现客户端服务器端之间的网络通信。下面是一个简单的例子: 服务器端: ```java import java.net.*; import java.io.*; public class Server { public static void main(String[] args) throws IOException { ServerSocket serverSocket = null; try { serverSocket = new ServerSocket(8888); } catch (IOException e) { System.err.println("Could not listen on port: 8888."); System.exit(1); } Socket clientSocket = null; try { System.out.println("Waiting for connection..."); clientSocket = serverSocket.accept(); System.out.println("Connection established with: " + clientSocket.getInetAddress().getHostAddress()); } catch (IOException e) { System.err.println("Accept failed."); System.exit(1); } PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true); BufferedReader in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream())); String inputLine, outputLine; while ((inputLine = in.readLine()) != null) { System.out.println("Received message: " + inputLine); outputLine = "Server received message: " + inputLine; out.println(outputLine); if (outputLine.equals("bye")) break; } out.close(); in.close(); clientSocket.close(); serverSocket.close(); } } ``` 客户端: ```java import java.net.*; import java.io.*; public class Client { public static void main(String[] args) throws IOException { Socket socket = null; PrintWriter out = null; BufferedReader in = null; try { socket = new Socket("localhost", 8888); out = new PrintWriter(socket.getOutputStream(), true); in = new BufferedReader(new InputStreamReader(socket.getInputStream())); } catch (UnknownHostException e) { System.err.println("Unknown host: localhost."); System.exit(1); } catch (IOException e) { System.err.println("Couldn't get I/O for the connection to: localhost."); System.exit(1); } BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in)); String userInput; while ((userInput = stdIn.readLine()) != null) { out.println(userInput); String serverResponse = in.readLine(); System.out.println("Server response: " + serverResponse); if (serverResponse.equals("bye")) break; } out.close(); in.close(); stdIn.close(); socket.close(); } } ``` 以上代码实现了一个简单的客户端服务器端通信,可以通过命令行输入消息进行交互。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值