对于TCP编程常常有下面几种情况
1. 服务端给客户端一个反馈的案例
在服务器端收到客户端的数据后,反馈给客户端提示数据已经收到
ServerDemo.java
public class ServerDemo {
public static void main(String[] args) throws IOException {
ServerSocket ss = new ServerSocket(10086);
Socket s = ss.accept();
//读取客户端的数据
InputStream in = s.getInputStream();
byte[] bys = new byte[1024];
int len = in.read(bys);
String str = new String(bys, 0, len);
System.out.println("客户端数据是" + str);
//向客户端反馈数据
OutputStream ou = s.getOutputStream();
ou.write("我已经接受到了数据".getBytes());
s.close();
// ss.close();//服务器是不能够关闭的,万一还有其他客户端需要访问服务器的数据
}
}
ClientDemo.java
public class ClientDemo {
public static void main(String[] args) throws IOException {
Socket s = new Socket("192.168.1.107", 10086);
// 向服务端写数据
OutputStream out = s.getOutputStream();
out.write("今天天气不好".getBytes());
// 从服务端获取数据
InputStream in = s.getInputStream();
byte[] bys = new byte[1024];
int len = 0;
len = in.read(bys);
String str = new String(bys, 0, len);
System.out.println(str);
s.close();
}
}
2. TCP协议上传文本文件
UploadClient.java
public class UploadClient {
public static void main(String[] args) throws IOException {
// 创建客户端Socket对象
Socket s = new Socket("192.168.1.107", 10086);
// 封装文本文件
BufferedReader br = new BufferedReader(
new FileReader("ClientDemo.java"));
// 封装通道内流
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(
s.getOutputStream()));
String line = null;
while ((line = br.readLine()) != null) {
bw.write(line);
bw.newLine();
bw.flush();
}
br.close();
s.close();
}
}
UploadServer.java
public class UpLoadServer {
public static void main(String[] args) throws IOException {
// 创建服务器端的Socket对象
ServerSocket ss = new ServerSocket(10086);
// 监听客户端连接
Socket s = ss.accept();
// 封装文本文件
BufferedWriter bw = new BufferedWriter(new FileWriter("bw.txt"));
// 封装通道内的流
BufferedReader br = new BufferedReader(new InputStreamReader(
s.getInputStream()));
String line = null;
while ((line = br.readLine()) != null) {
bw.write(line);
bw.newLine();
bw.flush();
}
bw.close();
s.close();
}
}
3. TCP上传文件并给出反馈
在Socket中有一个方法
void shutDownOutput():禁用此套接字的输出流。对于 TCP 套接字,任何以前写入的数据都将被发送,并且后跟 TCP 的正常连接终止序列。 如果在套接字上调用 shutdownOutput() 后写入套接字输出流,则该流将抛出 IOException。
所以在上传文件给服务器时,上传完成后,我们必须关掉输出流,然后再在服务器端写入想要反馈的数据来客户端接收
客户端示例:
UploadClient.java
public class UploadClient {
public static void main(String[] args) throws IOException {
// 创建客户端Socket对象
Socket s = new Socket("192.168.1.107", 10086);
// 封装文本文件
BufferedReader br = new BufferedReader(
new FileReader("ClientDemo.java"));
// 封装通道内流
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(
s.getOutputStream()));
String line = null;
while ((line = br.readLine()) != null) {
bw.write(line);
bw.newLine();
bw.flush();
}
s.shutdownOutput();
br.close();
InputStream in = s.getInputStream();
byte[] bys = new byte[1024];
int len = 0;
len = in.read(bys);
String str = new String(bys, 0, len);
System.out.println(str);
s.close();
}
}
4. 多个客户端公用服务器上传文件
在现实生活中,多个客户端上传到服务端是很常见的问题,客户端我们采用多线程的方式
UserThread.java
public class UserThread implements Runnable {
private Socket s;
public UserThread(Socket s) {
this.s = s;
}
@Override
public void run() {
try {
// 封装通道内的流
BufferedReader br = new BufferedReader(new InputStreamReader(
s.getInputStream()));
// 封装文本文件
// BufferedWriter bw = new BufferedWriter(new
// FileWriter("Copy.java"));
// 为了防止名称冲突
String newName = System.currentTimeMillis() + ".java";
BufferedWriter bw = new BufferedWriter(new FileWriter(newName));
String line = null;
while ((line = br.readLine()) != null) { // 阻塞
bw.write(line);
bw.newLine();
bw.flush();
}
// 给出反馈
BufferedWriter bwServer = new BufferedWriter(
new OutputStreamWriter(s.getOutputStream()));
bwServer.write("文件上传成功");
bwServer.newLine();
bwServer.flush();
// 释放资源
bw.close();
s.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
UploadServer.java
public class UploadServer {
public static void main(String[] args) throws IOException {
// 创建服务器Socket对象
ServerSocket ss = new ServerSocket(11111);
while (true) {
Socket s = ss.accept();
new Thread(new UserThread(s)).start();
}
}
}