Java Socket编程实例(一)- TCP基本使用
作者:kingxss 时间:2023-11-11 08:42:50
一.服务端代码:
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
public static void main(String[] args) throws IOException {
int servPort = 5500;
// Create a server socket to accept client connection requests
ServerSocket servSock = new ServerSocket(servPort);
int recvMsgSize; // Size of received message
byte[] receiveBuf = new byte[BUFSIZE]; // Receive buffer
while (true) { // Run forever, accepting and servicing connections
Socket clntSock = servSock.accept(); // Get client connection
SocketAddress clientAddress = clntSock.getRemoteSocketAddress();
System.out.println("Handling client at " + clientAddress);
InputStream in = clntSock.getInputStream();
OutputStream out = clntSock.getOutputStream();
// Receive until client closes connection, indicated by -1 return
while ((recvMsgSize = in.read(receiveBuf)) != -1) {
out.write(receiveBuf, 0, recvMsgSize);
}
clntSock.close(); // Close the socket. We are done with this client!
}
/* NOT REACHED */
}
}
二.客户端代码:
import java.net.*;
import java.io.*;
public class TCPEchoClient {
public static void main(String[] args) throws IOException {
String server = "127.0.0.1"; // Server name or IP address
int servPort = 5500; //// Server port
byte[] data = "Hi, World".getBytes();
// 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(data); // 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 < data.length) {
if ((bytesRcvd = in.read(data, totalBytesRcvd, data.length - totalBytesRcvd)) == -1)
throw new SocketException("Connection closed prematurely");
totalBytesRcvd += bytesRcvd;
} // data array is full
System.out.println("Received: " + new String(data));
socket.close(); // Close the socket and its streams
}
}
上述代码的TCP服务端是单线程,一次只能服务一个客户端。
查看更多Java的语法,大家可以关注:《Thinking in Java 中文手册》、《JDK 1.7 参考手册官方英文版》、《JDK 1.6 API java 中文参考手册》、《JDK 1.5 API java 中文参考手册》,也希望大家多多支持脚本之家。
标签:Java,Socket,TCP
0
投稿
猜你喜欢
java多线程之铁路售票系统
2022-01-31 12:25:14
Java并发编程之Java内存模型
2023-08-17 23:06:10
Java 六类运算符详解
2023-08-27 20:37:37
java判断字符串是否为数字的方法小结
2023-11-25 05:54:52
javaWeb项目部署到阿里云服务器步骤详解
2023-11-07 05:21:36
基于C#中XmlWriter写入Xml的深入分析
2021-06-29 10:00:54
Java中的ArrayList容量及扩容方式
2023-10-17 17:24:40
C#交错数组用法实例
2022-08-13 09:34:55
Activiti开发环境的配置
2021-07-31 21:57:51
Java使用动态规划算法思想解决背包问题
2022-12-02 03:53:49
Java实现导出ZIP压缩包的方法
2023-05-06 04:58:57
Struts2学习教程之拦截器机制与自定义拦截器
2021-10-27 13:02:56
C# 拷贝数组的几种方法(总结)
2023-09-14 06:50:17
Spring中使用atomikos+druid实现经典分布式事务的方法
2023-07-14 00:46:17
Android 背景透明度设置总结
2023-06-06 04:31:13
springboot集成Swagger的方法(让你拥有属于自己的api管理器)
2023-11-08 20:39:15
C#实现围棋游戏
2022-04-20 11:08:57
Javaweb El表达式实例详解
2021-06-21 22:59:32
java反射获取包下所有类的操作
2022-02-12 16:06:15
Hook实现Android 微信、陌陌 、探探位置模拟(附源码下载)
2022-11-14 14:44:43