Java 网络编程Tcp

时间:2022-04-06 20:18:17

Java 利用网络编程有两种方式:一种是Tcp,另一种是Udp

下面我用Tcp编程举例:

/*
编程实现:
1.使用TCP协议发送数据,TCP协议接收数据
2.使用TCP协议发送数据(键盘录入),TCP协议接收数据,并将其显示在控制台
3.使用TCP协议发送数据(文本文件),TCP协议接收数据,并将其显示在控制台
4.使用TCP协议发送数据(键盘录入),TCP协议接收数据,并将数据写入文本文件
5.使用TCP协议发送数据(文本文件),TCP协议接收数据,并将数据写入文本文件
*/

import java.net.Socket;
import java.io.OutputStream;
import java.io.IOException;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.BufferedWriter;
import java.io.OutputStreamWriter;
import java.io.FileReader;
import java.io.InputStream;


class TcpSendDemo{
public static void main(String[] args) throws IOException{
//method1();
//method2();
//method3();
//method4();
method5();
}

//1.使用TCP协议发送数据,TCP协议接收数据
public static void method1() throws IOException{
Socket s = new Socket("202.193.52.73",8888);

OutputStream os = s.getOutputStream();

os.write("hello,Tcp,我来了".getBytes());

InputStream is = s.getInputStream();

byte[] bys = new byte[1024];
int len = is.read(bys);

System.out.println(new String(bys,0,len));

//os.close();
s.close();
}

//2.使用TCP协议发送数据(键盘录入),TCP协议接收数据,并将其显示在控制台
public static void method2() throws IOException{

Socket s = new Socket("202.193.52.73",8888);

BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));

String line = null;


while((line = br.readLine()) != null){
if(line.equals("886")){
break;
}
bw.write(line);
bw.newLine();
bw.flush();
}

br.close();

//BufferedReader br1 = new BufferedReader(new InputStreamReader(s.getInputStream()));
//String line1 = null;
//String line1 = br1.readLine();

//while((line1 = br1.readLine()) != null){

//System.out.println(line1);
//}


s.close();
}

//3.使用TCP协议发送数据(文本文件),TCP协议接收数据,并将其显示在控制台
public static void method3() throws IOException{

Socket s = new Socket("202.193.52.73",8888);

BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
BufferedReader br = new BufferedReader(new FileReader("E:\\PS.txt"));
String line = null;

while((line = br.readLine()) != null){

bw.write(line);
bw.newLine();
bw.flush();
}
s.shutdownOutput();

BufferedReader br1 = new BufferedReader(new InputStreamReader(s.getInputStream()));
System.out.println(br1.readLine());
br.close();
bw.close();
}

//4.使用TCP协议发送数据(键盘录入),TCP协议接收数据,并将数据写入文本文件
public static void method4() throws IOException{

Socket s = new Socket("202.193.52.73",8888);

BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));

String line = null;


while((line = br.readLine()) != null){
if(line.equals("886")){
break;
}
bw.write(line);
bw.newLine();
bw.flush();
}
s.shutdownOutput();

BufferedReader br1 = new BufferedReader(new InputStreamReader(s.getInputStream()));
System.out.println(br1.readLine());

bw.close();
s.close();
}

//5.使用TCP协议发送数据(文本文件),TCP协议接收数据,并将数据写入文本文件
public static void method5() throws IOException{

Socket s = new Socket("202.193.52.73",8888);

BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
BufferedReader br = new BufferedReader(new FileReader("E:\\PS.txt"));
String line = null;

while((line = br.readLine()) != null){

bw.write(line);
bw.newLine();
bw.flush();
}
s.shutdownOutput();

BufferedReader br1 = new BufferedReader(new InputStreamReader(s.getInputStream()));
System.out.println(br1.readLine());
//bw.write("over");
//bw.newLine();
//bw.flush();

br.close();
bw.close();
}
}