java 接收端接收控制台输入内容

来源:互联网 发布:控制面板windows功能 编辑:程序博客网 时间:2024/06/06 03:05


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;

pubblic class Send{

public static void main(String[] args) throws Exception{

//发送端

DataprogramSocket ds = new DataprogramSocket();

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

String str = null;

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

byte[] bytes=str.getBytes();

DataprogramPacket dp = newDataprogramPacket(bytes,bytes,length,InetAddress.getByname("127.0.0.1"),7777);

ds.send(dp);

}

ds.close();

}

}



import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;

public class Receive{

public static void main(String[] args) throws IOException{

//接收端

DataprogramSoket ds = new DataprogramSocket(7777);

while(true){

byte[] bytes = new byte[1024];

DataprogramPacket dp = new DataprogramPacket(bytes,bytes,length);

ds.receive(dp);

//解析数据包

byte[] buf = dp.getData();

String str = new String(buf,0,buf.length);

System.out.println("from:("+dp.getAddress().getHostName()+")"+str);

}

}

}

0 0
原创粉丝点击