UDP协议
不管接收端是否存在都会执行发生操作,并且接收端不会做出响应,`
public class Demo {
public static void main(String[] args) throws IOException {
//DatagramSocket封装了upd协议的相关方法
DatagramSocket ds = new DatagramSocket();
String s = "hello";
InetAddress ip = InetAddress.getByName("127.0.0.1");
//字节数组,长度,长度,ip,端口号
System.out.println(ip);
DatagramPacket dp= new DatagramPacket(s.getBytes(),0,s.getBytes().length,ip,8888);
ds.send(dp);//发送要发的数据
ds.close();
}
}
public class Receive {
public static void main(String[] args) throws IOException {
//服务器没有指定ip默认本地主机,要指定端口号
DatagramSocket ds = new DatagramSocket(8888);
byte[] bytes = new byte[1024];//通过字节数组接受客户端的数据
DatagramPacket dp = new DatagramPacket(bytes,0,bytes.length);
ds.receive(dp);
// String s = new String(bytes,0,bytes.length);
String s2 = new String(bytes,0,bytes.length);
System.out.println(s2);
ds.close();
}
}
客户端从键盘输入数据服务端接受输出控制台
//客户端
public class SendDemo {
public static void main(String[] args)throws Exception{
DatagramSocket ds = new DatagramSocket();
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String line;
InetAddress ip = InetAddress.getByName("127.0.0.1");
while ((line=br.readLine())!=null){
DatagramPacket p = new DatagramPacket(line.getBytes(),0,line.getBytes().length,ip,8888);
ds.send(p);
if (line.equals("haha")){
break;
}
}
}
}
//接收端
public class ReceiveDemo {
public static void main(String[] args)throws Exception{
DatagramSocket ds = new DatagramSocket(8888);
while (true){
byte[] bytes = new byte[1024];
DatagramPacket p = new DatagramPacket(bytes,0,bytes.length);
ds.receive(p);
System.out.println(new String(bytes,0,bytes.length));
}
}
}
TCP
会对客户端发生的数据做出回应
public class Send {
public static void main(String[] args)throws Exception{
//Socket类封装了TCP协议的相关方法,代表TCP客户端
Socket s = new Socket("127.0.0.1",8888);
//获取字节输出流
OutputStream os = s.getOutputStream();
os.write("你好TCP".getBytes());
os.close();
s.close();
}
}
public static void main(String[] args)throws Exception{
ServerSocket ss = new ServerSocket(8888);
//accept方法表示接受客户端的连接
Socket accept = ss.accept();
InputStream is = accept.getInputStream();
byte[] bytes = new byte[1024];
int read = is.read(bytes);
System.out.println(new String(bytes,0, read));
}
}
TCP客户端接收服务器返回的数据
public class Send {
public static void main(String[] args)throws Exception{
Socket s = new Socket("127.0.0.1", 6666);
//获取字节输出流,向服务端写数据
OutputStream os = s.getOutputStream();
//开始写数据
os.write("我想服务器端写数据".getBytes());
//接受服务器返回的数据,获取输入流读取数据
InputStream is = s.getInputStream();
byte[] bytes = new byte[1024];
int read = is.read(bytes);
System.out.println(new String(bytes,0,read));
is.close();
os.close();
s.close();
}
}
public class Receive {
public static void main(String[] args)throws Exception{
ServerSocket ss = new ServerSocket(6666);
Socket accept = ss.accept();
//获取字节输入流读取客户端的数据
InputStream is = accept.getInputStream();
byte[] bytes = new byte[1024];
int read = is.read(bytes);
System.out.println(new String(bytes,0,read));
//写数据返回给客户端做出回应
//获取字节输出流向客户端写数据
OutputStream os = accept.getOutputStream();
os.write("你好我收到你的数据了".getBytes());
accept.close();
ss.close();
}
}
TCP客户端键盘录入数据发送到服务器写入文件
public class Send {
public static void main(String[] args)throws Exception{
Socket s = new Socket("127.0.0.1", 1111);
OutputStream os = s.getOutputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String line;
while ((line=br.readLine())!=null){
os.write(line.getBytes());
}
}
}
public class Receive {
public static void main(String[] args)throws Exception{
ServerSocket ss = new ServerSocket(1111);
Socket accept = ss.accept();
InputStream is = accept.getInputStream();
int len;
byte[] bytes = new byte[1024];
FileOutputStream fos = new FileOutputStream("D:\\Idea-workplace\\寒假复习\\网络编程\\d.txt");
while ((len=is.read(bytes))!=-1){
if (new String(bytes,0,len).equals("haha")){
break;
}
fos.write(bytes,0,len);
fos.flush();
}
}
}
TCP客户端读取本地文件服务器保存在文件夹中
public class Send {
public static void main(String[] args)throws Exception{
Socket s = new Socket("127.0.0.1", 1111);
OutputStream os = s.getOutputStream();
FileInputStream is = new FileInputStream("D:\\Idea-workplace\\寒假复习\\网络编程\\静夜思.txt");
int len;
byte[] bytes =new byte[1024];
while ((len=is.read(bytes))!=-1){
os.write(bytes,0,len);
}
//告诉服务器数据发送完毕
s.shutdownOutput();
InputStream is1 = s.getInputStream();
byte[] bytes1 = new byte[1024];
int read = is1.read(bytes1);
System.out.println(new String(bytes1,0,read));
}
}
public class Receive {
public static void main(String[] args)throws Exception{
ServerSocket ss = new ServerSocket(1111);
Socket accept = ss.accept();
InputStream is = accept.getInputStream();
int len;
byte[] bytes = new byte[1024];
OutputStream os = new FileOutputStream("D:\\Idea-workplace\\寒假复习\\网络编程\\jys.txt");
while ((len=is.read(bytes))!=-1){
os.write(bytes,0,len);
os.write("\r\n".getBytes());
os.flush();
}
System.out.println("-----------------------------");
//做出回应
OutputStream os1 = accept.getOutputStream();
os1.write("好的我收到你的数据了".getBytes());
}
}
//\r\n是换行符号
来源:CSDN
作者:無觞
链接:https://blog.csdn.net/qq_45123018/article/details/104076645