黑马程序员技术交流社区
标题:
请问UDP写聊天程序,为什么接收不到数据呢
[打印本页]
作者:
Cola
时间:
2014-1-13 21:30
标题:
请问UDP写聊天程序,为什么接收不到数据呢
本帖最后由 Cola 于 2014-1-14 20:18 编辑
用UDP写的聊天程序,接收类Rece接收不到port。传的是10010,Rece类中的port是-1,请问为什么呢?
class Rece implements Runnable
{
private DatagramSocket ds;
public Rece(DatagramSocket ds)
{
this.ds=ds;
}
public void run()
{
try
{
while(true)
{
System.out.println("Rece");
System.out.println("port "+ds.getPort());
byte[] buf=new byte[1024];
DatagramPacket dp=new DatagramPacket(buf,buf.length);
ds.receive(dp);
System.out.println("Receive dp");
String ip=dp.getAddress().getHostAddress();
String data=new String(dp.getData(),0,dp.getLength());
if("886".equals(data))
{
System.out.println(ip+"......离开聊天室");
break;
}
System.out.println(ip+"::"+data);
}
}
catch (Exception e)
{
throw new RuntimeException("接收端失败");
}
}
}
class ChatDemo2
{
public static void main(String[] args) throws Exception
{
//DatagramSocket sendSocket=new DatagramSocket();
DatagramSocket receSocket=new DatagramSocket(10010);
//new Thread(new Send(sendSocket)).start();
new Thread(new Rece(receSocket)).start();
}
}
复制代码
捕获.JPG
(15.88 KB, 下载次数: 46)
下载附件
2014-1-13 21:29 上传
运行结果
作者:
几番意难相付
时间:
2014-1-13 22:27
重新看看毕姥爷第23天的第八节课的视频吧
作者:
程玉习
时间:
2014-1-14 01:27
class Rece implements Runnable
{
private DatagramSocket ds ;
public Rece(DatagramSocket ds )
{
this.ds = ds;
}
public void run()
{
try
{
while(true)
{
byte[] buf = new byte[1024];
DatagramPacket dp = new DatagramPacket(buf,buf.length);
ds.receive(dp);
String ip = dp.getAddress().getHostAddress();
String data = new String(dp.getData(),0,dp.getLength());
System.out.println("(" + ip + "):" + data);
}
}
catch (Exception e)
{
throw new RuntimeException("接收失败");
}
}
}
class ChatDemo
{
public static void main(String[] args) throws Exception
{
//创建Send 和 Rece 的DatagramSocket对象,并指定监听的端口。
DatagramSocket senfDs = new DatagramSocket(6666);
DatagramSocket receDs = new DatagramSocket(8888);
//开启线程,进行通讯。
new Thread(new Send(senfDs)).start();
new Thread(new Rece(receDs)).start();
}
}
复制代码
作者:
汪伟楠
时间:
2014-1-14 10:52
这是我刚写的,你看看对你有什么帮助没!
import java.io.*;
import java.net.*;
class Send implements Runnable
{
private DatagramSocket ds;
public Send(DatagramSocket ds)
{
this.ds = ds;
}
public void run()
{
try
{
BufferedReader bu = new BufferedReader(new InputStreamReader(System.in));
String line = null;
while((line =bu.readLine())!=null)
{
if ("886".equals(line))
break;
byte[] bf = line.getBytes();
DatagramPacket dp
= new DatagramPacket(bf,bf.length,InetAddress.getByName("127.0.0.1"),10002);
ds.send(dp);
}
ds.close();
}
catch (Exception e)
{
throw new RuntimeException("发送失败");
}
}
}
class Rece implements Runnable
{
private DatagramSocket ds;
public Rece(DatagramSocket ds)
{
this.ds = ds;
}
public void run()
{
try
{
while(true)
{
byte[] buf = new byte[1024];
DatagramPacket dp = new DatagramPacket(buf,buf.length);
ds.receive(dp);
String ip = dp.getAddress().getHostAddress();
String data=new String(dp.getData(),0,dp.getLength());
int port = dp.getPort();
System.out.println(ip+"---"+data+"-----"+port);
}
}
catch (Exception e)
{
throw new RuntimeException("接收失败");
}
}
}
class ChatDemo
{
public static void main(String[] args) throws Exception
{
DatagramSocket sendSocket = new DatagramSocket();
DatagramSocket receSocket = new DatagramSocket(10002);
new Thread (new Send(sendSocket)).start();
new Thread (new Rece(receSocket)).start();
}
}
复制代码
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2