JAVASE基础模块四十一(TCP连接 键盘录入TCP连接发送消息 将TCP客户端发送内容写到文件 将文件内容传给服务端并输出文件内容 TCP传输文件 一个服务器连接多个客户端)
TCP
客户端 Socket
- 概念
此类实现客户端套接字(也可以就叫“套接字”)。套接字是两台机器间通信的端点。
套接字的实际工作由 SocketImpl 类的实例执行。应用程序通过更改创建套接字实现的套接字工厂可以配置它自身,以创建适合本地防火墙的套接字。
- 构造方法
Socket() 通过系统默认类型的 SocketImpl 创建未连接套接字
- 方法
void close() 关闭此套接字。
- 代码
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
public class TCPCli {
public static void main(String[] args) {
try {
Socket s = new Socket("192.168.17.201", 7777);
OutputStream o = s.getOutputStream();
InputStream in = s.getInputStream();
String w = "却道天凉好个秋";
o.write(w.getBytes());
byte[] bytes = new byte[1024];
int len = in.read(bytes);
String s1 = new String(bytes, 0, len);
System.out.println(s1);
s.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
服务端 ServerSocket
- 概念
此类实现服务器套接字。服务器套接字等待请求通过网络传入。它基于该请求执行某些操作,然后可能向请求者返回结果。
服务器套接字的实际工作由 SocketImpl 类的实例执行。应用程序可以更改创建套接字实现的套接字工厂来配置它自身,从而创建适合本地防火墙的套接字
- 构造方法摘要
ServerSocket()
创建非绑定服务器套接字。
ServerSocket(int port)
创建绑定到特定端口的服务器套接字
- 方法
Socket accept() 侦听并接受到此套接字的连接。
- 步骤
- 创建服务端的连接
- 侦听客户端的连接 阻塞式方法 一直侦听客户端的连接
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
public class TCPS {
public static void main(String[] args) {
try {
ServerSocket s = new ServerSocket(7777);
System.out.println("服务器已连接.......");
Socket a = s.accept();
InputStream in = a.getInputStream();
OutputStream out = a.getOutputStream();
byte[] bytes = new byte[1024];
int len = in.read(bytes);
String h = a.getInetAddress().getHostName();
String s1 = new String(bytes, 0, len);
System.out.println(h + "发来了消息:" + s1);
String hu = "hello";
out.write(hu.getBytes());
s.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
- 运行结果
服务端:
服务器已连接.......
192.168.17.201发来了消息:却道天凉好个秋
进程已结束,退出代码0
客户端:
hello
进程已结束,退出代码0
键盘录入TCP发送消息
- 客户端
import java.io.*;
import java.net.Socket;
public class Tc {
public static void main(String[] args) {
try {
Socket s= new Socket("192.168.17.201", 5555);
OutputStream out = s.getOutputStream();
InputStream in = s.getInputStream();
BufferedReader b = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter w = new BufferedWriter(new OutputStreamWriter(out));
while (true){
System.out.println("input!!!!!!!!!");
String s1 = b.readLine();
w.write(s1);
w.newLine();
w.flush();
if ("died".equals(s1)){
break;
}
}
w.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
- 服务端
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
public class Ts {
public static void main(String[] args) {
try {
ServerSocket s = new ServerSocket(5555);
System.out.println("服务器已连接.......");
Socket a = s.accept();
InputStream in = a.getInputStream();
BufferedReader bu = new BufferedReader(new InputStreamReader(in));
String s1 = null;
while ((s1=bu.readLine())!=null){
System.out.println(s1);
if ("died".equals(s1)){
break;
}
}
s.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
- 运行结果
服务端:
服务器已连接.......
却道天凉好个秋
一树梨花压海棠
惶恐滩头说惶恐
零丁洋里叹零丁
died
进程已结束,退出代码0
客户端:
input!!!!!!!!!
却道天凉好个秋
input!!!!!!!!!
一树梨花压海棠
input!!!!!!!!!
惶恐滩头说惶恐
input!!!!!!!!!
零丁洋里叹零丁
input!!!!!!!!!
died
进程已结束,退出代码0
将TCP客户端发送内容写到文件
- 客户端
import java.io.*;
import java.net.Socket;
public class TPC1 {
public static void main(String[] args) {
try {
Socket s= new Socket("192.168.17.201", 5555);
OutputStream out = s.getOutputStream();
InputStream in = s.getInputStream();
BufferedReader b = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter w = new BufferedWriter(new OutputStreamWriter(out));
while (true){
System.out.println("input!!!!!!!!!");
String s1 = b.readLine();
w.write(s1);
w.newLine();
w.flush();
if ("died".equals(s1)){
break;
}
}
w.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
- 服务端
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
public class TPS2 {
public static void main(String[] args) {
try {
ServerSocket s = new ServerSocket(5555);
System.out.println("服务器已连接.......");
Socket a = s.accept();
InputStream in = a.getInputStream();
BufferedReader bu = new BufferedReader(new InputStreamReader(in));
BufferedWriter ww = new BufferedWriter(new FileWriter("F.txt"));
while (true){
String s1 = bu.readLine();
ww.write(s1);
ww.newLine();
ww.flush();
if ("died".equals(s1)){
break;
}
}
s.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
- 运行结果
服务端:无
客户端:
input!!!!!!!!!
却道天凉好个秋
input!!!!!!!!!
一树梨花压海棠
input!!!!!!!!!
123456789
input!!!!!!!!!
died
F.txt文件结果:
却道天凉好个秋
一树梨花压海棠
123456789
died
将文件内容传给服务端并输出文件内容
- 客户端
import java.io.*;
import java.net.Socket;
public class TCPFile {
public static void main(String[] args) {
try {
Socket s = new Socket("192.168.17.201", 5555);
OutputStream out = s.getOutputStream();
InputStream in = s.getInputStream();
BufferedReader b = new BufferedReader(new FileReader("ccc.md"));
BufferedWriter w = new BufferedWriter(new OutputStreamWriter(out));
System.out.println("input!!!!!!!!!");
String len = "";
while ((len = b.readLine()) != null) {
w.write(len);
w.newLine();
w.flush();
}
s.shutdownOutput();
System.out.println("over!!!!!");
byte[] bytes = new byte[1024];
int len1 = in.read();
System.out.println(new String(bytes, 0, len1));
System.out.println("over");
w.close();
System.out.println("over!!!!!");
} catch (IOException e) {
e.printStackTrace();
}
}
}
- 服务端
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
public class TCPSFile {
public static void main(String[] args) {
try {
ServerSocket s = new ServerSocket(5555);
System.out.println("服务器已连接.......");
Socket a = s.accept();
InputStream in = a.getInputStream();
BufferedReader bu = new BufferedReader(new InputStreamReader(in));
BufferedWriter ww = new BufferedWriter(new FileWriter("fff.md"));
String s1 =null;
while ((s1=bu.readLine())!=null){
System.out.println(s1);
ww.write(s1);
ww.newLine();
ww.flush();
}
out.write("接受完成!".getBytes());
System.out.println("over");
s.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
- 运行结果
服务端:
服务器已连接.......
# JAVASE基础模块四十(UDP连接)
# 网络编程三要素
....略
Process finished with exit code 0
over
客户端:
input!!!!!!!!!
over!!!!!
over
文件结果:
# JAVASE基础模块四十(UDP连接)
# 网络编程三要素
....略
Process finished with exit code 0
```
TCP传输文件
- 客户端
import java.io.*;
import java.net.Socket;
public class T1 {
public static void main(String[] args) {
try {
Socket s = new Socket("192.168.17.201", 5555);
OutputStream out = s.getOutputStream();
InputStream in = s.getInputStream();
FileInputStream b = new FileInputStream("xxx.mp3");
System.out.println("input!!!!!!!!!");
byte[] bytes = new byte[1024];
int len=0;
while ((len = b.read(bytes)) != -1) {
out.write(bytes,0,len);
out.flush();
}
out.close();
System.out.println("over!!!!!");
} catch (IOException e) {
e.printStackTrace();
}
}
}
- 服务端
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
public class T2 {
public static void main(String[] args) {
try {
ServerSocket s = new ServerSocket(5555);
System.out.println("服务器已连接.......");
Socket a = s.accept();
InputStream in = a.getInputStream();
FileOutputStream ww = new FileOutputStream("fff.mp3");
byte[] bytes = new byte[1024*8];
int len=0;
while ((len = in.read(bytes)) != -1) {
ww.write(bytes,0,len);
ww.flush();
}
System.out.println("over");
s.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
- 运行结果
客户端:
input!!!!!!!!!
over!!!!!
服务端:
服务器已连接.......
over
public class D {
public static void main(String[] args) {
File f1 = new File("fff.mp3");
File f2 = new File("xxx.mp3");
System.out.println(f1.length());
System.out.println(f2.length());
}
}
运行结果:
9465508
9465508
一个服务器连接多个客户端
- 服务端
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
public class Server {
public static void main(String[] args) {
try {
ServerSocket s = new ServerSocket(5555);
System.out.println("服务器已连接.......");
int i=1;
while (true){
Socket a = s.accept();
System.out.println((i++)+"已连接");
new UplTh(a).start();
}
//s.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
import java.io.*;
import java.net.Socket;
public class UplTh extends Thread{
private Socket a;
public UplTh(Socket a) {
this.a=a;
}
@Override
public void run() {
InputStream in = null;
try {
in = a.getInputStream();
OutputStream out = a.getOutputStream();
BufferedReader bu = new BufferedReader(new InputStreamReader(in));
BufferedWriter ww = new BufferedWriter(new FileWriter("ff.md"));
String s1 =null;
while ((s1=bu.readLine())!=null){
ww.write(s1);
ww.newLine();
ww.flush();
}
out.write("接受完成!".getBytes());
System.out.println("over");
} catch (IOException e) {
e.printStackTrace();
}
}
}
- 客户端
import java.io.*;
import java.net.Socket;
public class Cli {
public static void main(String[] args) {
try {
Socket s = new Socket("192.168.17.201", 5555);
OutputStream out = s.getOutputStream();
InputStream in = s.getInputStream();
BufferedReader b = new BufferedReader(new FileReader("ccc.md"));
BufferedWriter w = new BufferedWriter(new OutputStreamWriter(out));
System.out.println("input!!!!!!!!!");
String len = "";
while ((len = b.readLine()) != null) {
w.write(len);
w.newLine();
w.flush();
}
s.shutdownOutput();
System.out.println("over!!!!!");
byte[] bytes = new byte[1024];
int len1 = in.read();
System.out.println(new String(bytes, 0, len1));
System.out.println("over");
w.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
- 运行结果
服务器已连接.......
1已连接
over
2已连接
over
3已连接
over
4已连接
over
5已连接
over
6已连接
over
进程已结束,退出代码-1
待续…