TCP通信协议是一种可靠的网络协议,它在通信的两端各建立一个Socket对象,从而在通信的两端形成网络虚拟链路,一旦建立了虚拟的网络链路,两端的程序就可以通过虚拟链路进行通信
Java对基于TCP协议的的网络提供了良好的封装,使用Socket对象来代表两端的通信端口,并通过Socket产生O流来进行网络通信
public class ClientDemo {
public static void main(String[] args) throws IOException {
//创建客户端的Socket
//Socket s=new Socket(InetAddress.getByName("127.0.0.1"),10000);
Socket s=new Socket("127.0.0.1",10000);
//获取输出流写数据
OutputStream os=s.getOutputStream();
os.write("Hello TCP!".getBytes());
s.close();
}
}
public class ServerDemo {
public static void main(String[] args) throws IOException {
ServerSocket ss=new ServerSocket(10000);
Socket s=ss.accept();
InputStream is=s.getInputStream();
byte[] bys=new byte[1024];
int len=is.read(bys);
String data=new String(bys,0,len);
System.out.println(data);
s.close();
ss.close();
}
}
public class ClientDemo {
public static void main(String[] args) throws IOException {
//创建客户端的Socket
//Socket s=new Socket(InetAddress.getByName("127.0.0.1"),10000);
Socket s=new Socket("127.0.0.1",10000);
//获取输出流写数据
OutputStream os=s.getOutputStream();
os.write("Hello TCP!".getBytes());
//接收服务器的反馈
InputStream is=s.getInputStream();
byte[] bys=new byte[1024];
int len=is.read(bys);
String data=new String(bys,0,len);
System.out.println("客户端:"+data);
s.close();
}
}
public class ServerDemo {
public static void main(String[] args) throws IOException {
ServerSocket ss=new ServerSocket(10000);
Socket s=ss.accept();
InputStream is=s.getInputStream();
byte[] bys=new byte[1024];
int len=is.read(bys);
String data=new String(bys,0,len);
System.out.println("服务器:"+data);
//给反馈
OutputStream os=s.getOutputStream();
os.write("已经收到!".getBytes());
ss.close();
}
}
public class ClientDemo {
public static void main(String[] args) throws IOException {
//创建客户端的Socket
//Socket s=new Socket(InetAddress.getByName("127.0.0.1"),10000);
Socket s=new Socket("127.0.0.1",10000);
//数据来自键盘录入
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
//封装输出流对象,用字符流传输
BufferedWriter bw=new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
String line;
while((line=br.readLine())!=null){
if("886".equals(line))
break;
bw.write(line);
bw.newLine();
bw.flush();
// OutputStream os=s.getOutputStream();
// os.write(line.getBytes());
}
s.close();
}
}
public class ServerDemo {
public static void main(String[] args) throws IOException {
ServerSocket ss=new ServerSocket(10000);
Socket s=ss.accept();
InputStream is=s.getInputStream();
InputStreamReader isr=new InputStreamReader(is);
BufferedReader br=new BufferedReader(isr);
String line;
while((line=br.readLine())!=null){
System.out.println("服务器:"+line);
}
ss.close();
}
}
服务器将接收到的文本写入文件
public class ServerDemo {
public static void main(String[] args) throws IOException {
ServerSocket ss=new ServerSocket(10010);
Socket s=ss.accept();
InputStream is=s.getInputStream();
InputStreamReader isr=new InputStreamReader(is);
BufferedReader br=new BufferedReader(isr);
BufferedWriter bw=new BufferedWriter(new FileWriter("s.txt"));
String line;
while((line=br.readLine())!=null){
bw.write("服务器:"+line);
bw.newLine();
bw.flush();
}
bw.close();
ss.close();
}
}
public class ClientDemo {
public static void main(String[] args) throws IOException {
//创建客户端的Socket
//Socket s=new Socket(InetAddress.getByName("127.0.0.1"),10000);
Socket s=new Socket("127.0.0.1",10010);
//数据来自文本
BufferedReader br=new BufferedReader(new InputStreamReader(new FileInputStream("c.txt")));
//封装输出流对象,用字符流传输
BufferedWriter bw=new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
String line;
while((line=br.readLine())!=null){
if("886".equals(line))
break;
bw.write(line);
bw.newLine();
bw.flush();
}
br.close();
s.close();
}
}
public class ServerDemo {
public static void main(String[] args) throws IOException {
ServerSocket ss=new ServerSocket(10010);
Socket s=ss.accept();
InputStream is=s.getInputStream();
InputStreamReader isr=new InputStreamReader(is);
BufferedReader br=new BufferedReader(isr);
BufferedWriter bw=new BufferedWriter(new FileWriter("s.txt"));
String line;
while((line=br.readLine())!=null){
// if("886".equals(line)){
// break;
// }
bw.write("服务器:"+line);
bw.newLine();
bw.flush();
}
//给反馈
BufferedWriter bwServer=new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
bwServer.write("文件上传成功!");
bwServer.newLine();
bwServer.flush();
bw.close();
ss.close();
}
}
public class ClientDemo {
public static void main(String[] args) throws IOException {
//创建客户端的Socket
//Socket s=new Socket(InetAddress.getByName("127.0.0.1"),10000);
Socket s=new Socket("127.0.0.1",10010);
//数据来自文本
BufferedReader br=new BufferedReader(new FileReader("c.txt"));
BufferedReader bbr=new BufferedReader(new InputStreamReader(new FileInputStream("c.txt")));
//封装输出流对象,用字符流传输
BufferedWriter bw=new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
String line;
while((line=br.readLine())!=null){
bw.write(line);
bw.newLine();
bw.flush();
}
// //自定义结束标记
// bw.write("886");
// bw.newLine();
// bw.flush();
s.shutdownOutput();
//接受反馈
BufferedReader brClient =new BufferedReader(new InputStreamReader(s.getInputStream()));
String data=brClient.readLine();
System.out.println("服务器反馈:"+data);
br.close();
s.close();
}
}
出现问题:程序一直等待
public class ServerThread implements Runnable {
public Socket s;
public ServerThread(Socket s) {
this.s=s;
}
@Override
public void run() {
//接收数据,写入文本
try {
BufferedReader br=new BufferedReader(new InputStreamReader(s.getInputStream()));
BufferedWriter bw=new BufferedWriter(new FileWriter("src/com/itheima07/s.txt"));
String line;
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();
s.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
public class ServerDemo {
public static void main(String[] args) throws IOException {
//创建服务器端Socket
ServerSocket serverSocket=new ServerSocket(10010);
while(true){//监听客户端连接,返回一个Socket对象
Socket s = serverSocket.accept();
//为每个客户端开启一个线程
new Thread(new ServerThread(s)).start();
}
}
}
public class ClientDemo {
public static void main(String[] args) throws IOException {
//创建客户端的Socket
Socket s=new Socket("127.0.0.1",10010);
//数据来自文本
BufferedReader br=new BufferedReader(new FileReader("c.txt"));
BufferedReader bbr=new BufferedReader(new InputStreamReader(new FileInputStream("c.txt")));
//封装输出流对象,用字符流传输
BufferedWriter bw=new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
String line;
while((line=br.readLine())!=null){
bw.write(line);
bw.newLine();
bw.flush();
}
s.shutdownOutput();
//接受反馈
BufferedReader brClient =new BufferedReader(new InputStreamReader(s.getInputStream()));
String data=brClient.readLine();
System.out.println("服务器反馈:"+data);
br.close();
s.close();
}
}