UDP文字传输:
发送后记得关闭
客户发送输入的文字给服务器
public class Client {
public static void main(String[] args) throws Exception, UnknownHostException {
// TODO Auto-generated method stub
System.out.println("老子是客户");
String data="still learning still foolish";
//udp协议下的
DatagramSocket ds = new DatagramSocket(Global.C_PORT,InetAddress.getByName(Global.c_IP));
Scanner input=new Scanner(System.in);
System.out.println("请输入:");
String line=input.next();
byte[] bs=line.getBytes();
DatagramPacket dp = new DatagramPacket(bs,bs.length,InetAddress.getByName(Global.S_IP),Global.S_PORT);
ds.send(dp);
ds.close();
}
}
服务器接收:
public class Serv {
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
System.out.println("亲,您好,我是服务器");
DatagramSocket ds = new DatagramSocket(Global.S_PORT,InetAddress.getByName(Global.S_IP));
while(true){
//空包裹
byte[] bs=new byte[200];
DatagramPacket dp = new DatagramPacket(bs,bs.length);
ds.receive(dp);//阻塞方法,等待客户端上传“包裹”
//将数据解析
byte[] bss=dp.getData();
int len=dp.getLength();
int port=dp.getPort();
String ip=dp.getAddress().getHostAddress();//主机名地址即ip
String re=new String(bss,"utf-8");
String name=null;
System.out.println("主机"+ip+"说:"+re);
}
//ds.close();
}
}
存放ip和端口的类:
public class Global {
static final int C_PORT=13368;
static final int S_PORT=14758;
static String S_IP="192.168.1.170";
static String c_IP="192.168.1.170";
}
双人聊天:
通过A发送B接收 A的send()对应B的receive() 且两个都要是B的端口和ip
通过B发送A接收 B的send()对应A的receive() 且两个都要是A的端口和ip
注意A中send和receive中自己的ip和端口要不同,否则,线程启动会端口重复异常
新建send类 和 receive类 来implements Runnable 在主方法中定义Thread采用这两类的匿名对象,实现聊天。
注释:Global沿用上面的类。
(有空改进一下,两个代码冗余)
public class Test {
public static void main(String args[]){
new Thread(new SendSocket()).start();
new Thread(new ReceiveSocket()).start();
}
public static class SendSocket implements Runnable{
@Override
public void run() {
// TODO Auto-generated method stub
Scanner cin=new Scanner(System.in);
DatagramSocket sendSocket=null;
DatagramPacket packet=null;
while(cin.hasNext()){
try {
// System.out.println("↑↑↑↑send:"); //System.out.println();
byte[] b = cin.next().getBytes("utf-8"); //接收方2的端口9387
packet = new DatagramPacket(b, b.length, InetAddress.getByName(Global.S_IP), 9337);//接收方2IP地址
sendSocket = new DatagramSocket(9307);//发送人1的用户信息
sendSocket.send(packet);
sendSocket.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (cin != null)
cin.close();
if (sendSocket != null)
sendSocket.close();
}
}
public static class ReceiveSocket implements Runnable{
@Override
public void run() {
// TODO Auto-generated method stub
while(true){
byte[] b = new byte[1024];
DatagramPacket packet = new DatagramPacket(b, b.length);
DatagramSocket receiveSocket = null;
try {
receiveSocket = new DatagramSocket(9317); //接收人1的端口,保证与发送端口不一样
receiveSocket.receive(packet);
String text = new String(packet.getData(), 0, packet.getLength(),"utf-8");
System.out.println("BBBBB说: "+packet.getAddress()+ ": " + text);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
receiveSocket.close();
}
}
}
}
}
public class Test2 {
public static void main(String args[]){
new Thread(new SendSocket()).start();
new Thread(new ReceiveSocket()).start();
}
public static class SendSocket implements Runnable{
@Override
public void run() {
// TODO Auto-generated method stub
Scanner cin=new Scanner(System.in);
DatagramSocket sendSocket=null;
DatagramPacket packet=null;
while(cin.hasNext()){
try {
// System.out.println("↑↑↑↑send:"); //System.out.println();
byte[] b = cin.next().getBytes("utf-8"); //接收方1的端口9317
packet = new DatagramPacket(b, b.length, InetAddress.getByName(Global.S_IP), 9317);//接收方1IP地址
sendSocket = new DatagramSocket(9387);//发送人的用户信息
sendSocket.send(packet);
sendSocket.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (cin != null)
cin.close();
if (sendSocket != null)
sendSocket.close();
}
}
public static class ReceiveSocket implements Runnable{
@Override
public void run() {
// TODO Auto-generated method stub
while(true){
byte[] b = new byte[1024];
DatagramPacket packet = new DatagramPacket(b, b.length);
DatagramSocket receiveSocket = null;
try {
receiveSocket = new DatagramSocket(9337,InetAddress.getByName(Global.S_IP));
//监听发送人1的信息,接收人2自己的端口,注意这个端口和发送的端口要不一样,否则两个端口起冲突
receiveSocket.receive(packet);
String text = new String(packet.getData(), 0, packet.getLength(),"utf-8");
System.out.println("AAAA 说:"+packet.getAddress()+ ": " + text);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
receiveSocket.close();
}
}
}
}
}