黑马程序员技术交流社区

标题: DOS对话的问题 [打印本页]

作者: 王雷1    时间: 2013-11-26 10:50
标题: DOS对话的问题
本帖最后由 王雷1 于 2013-11-26 14:20 编辑

//我这么写是因为都是在127.0.0.1这一台机器上所以出现了冲突所以无法运行的还是为什么?
//还是根本就不能这么写?我是想实现一下dos的聊天,用一台电脑能实现么?如果ok应该是什么样的?
import java.net.*;
import java.io.*;
class er
{
public static void main(String[] args)throws Exception
{
  jieshou();
  fasong();
}
public static void jieshou()throws Exception
{
  DatagramSocket ds=new DatagramSocket(10001);
  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);
   
  }
}
public static void fasong()throws Exception
{
  
  DatagramSocket ds=new DatagramSocket();
  
  BufferedReader bufr=new BufferedReader(new InputStreamReader(System.in));
  String line=null;
  while((line=bufr.readLine())!=null)
  {
   if("886".equals(line))
    break;
   byte[] buf=line.getBytes();
   DatagramPacket dp=new DatagramPacket(buf,buf.length,InetAddress.getByName("127.0.0.1"),10002);
   ds.send(dp);
  }
}
}
class yi
{
public static void main(String[] args)throws Exception
{
  jieshou();
  fasong();
}
public static void jieshou()throws Exception
{
  
  DatagramSocket ds=new DatagramSocket(10002);
  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);
   
  }
}
public static void fasong()throws Exception
{
  
  DatagramSocket ds=new DatagramSocket();
  
  BufferedReader bufr=new BufferedReader(new InputStreamReader(System.in));
  String line=null;
  while((line=bufr.readLine())!=null)
  {
   if("886".equals(line))
    break;
   byte[] buf=line.getBytes();
   DatagramPacket dp=new DatagramPacket(buf,buf.length,InetAddress.getByName("127.0.0.1"),10001);
   ds.send(dp);
  }
}
}


作者: 王雷1    时间: 2013-11-26 11:04
{:soso_e113:}{:soso_e113:}{:soso_e113:}{:soso_e113:}{:soso_e113:}
作者: freehiker    时间: 2013-11-26 11:12
我把我写的练习发上来吧,可以实现同一台电脑聊天,但是需要通过cmd命令执行class,不能通过eclipse
第一个客户端:

  1. /*
  2. * 练习:
  3. * 通过多线程、键盘录入、Socket建立一个Chat程序,初步沟通没问题后,进行图形化修改
  4. * 思路:
  5. * 1.通一个进程中同时需要发送和接收信息,所以需要用到多线程
  6. * 2.一个线程负责发送信息,一个线程负责接收信息
  7. */
  8. import java.io.BufferedReader;
  9. import java.io.IOException;
  10. import java.io.InputStreamReader;
  11. import java.net.DatagramPacket;
  12. import java.net.DatagramSocket;
  13. import java.net.InetAddress;
  14. import java.net.SocketException;
  15. public class UdpOne {

  16.         public static void main(String[] args) {
  17.                 // TODO Auto-generated method stub
  18.                 try
  19.                 {
  20.                         //DatagramSocket send = new DatagramSocket();
  21.                         DatagramSocket receive = new DatagramSocket(10002);
  22.                         new Thread(new Send(receive)).start();
  23.                         new Thread(new Receive(receive)).start();
  24.                 }
  25.                 catch (SocketException se)
  26.                 {
  27.                         throw new RuntimeException("建立数据包套接字失败");
  28.                 }
  29.                
  30.         }

  31. }

  32. //TODO Send
  33. class Send implements Runnable
  34. {
  35.         private DatagramSocket ds;
  36.         Send(DatagramSocket ds)
  37.         {
  38.                 this.ds = ds;
  39.         }
  40.         public void run()
  41.         {
  42.                 try
  43.                 {
  44.                         BufferedReader bfr = new BufferedReader(new InputStreamReader(System.in));
  45.                         String line = null;
  46.                         while ((line=bfr.readLine())!=null)
  47.                         {
  48.                                 if(line.equals("over"))
  49.                                         break;
  50.                                 byte[] buf = line.getBytes();
  51.                                 DatagramPacket dp = new DatagramPacket(buf,buf.length,
  52.                                                 InetAddress.getByName("192.168.2.105"),10001);
  53.                                 ds.send(dp);
  54.                                 //System.out.println(line.toUpperCase());
  55.                         }
  56.                         bfr.close();
  57.                         ds.close();
  58.                 }
  59.                 catch (IOException e)
  60.                 {
  61.                         throw new RuntimeException("发送失败");
  62.                 }
  63.         }
  64. }

  65. //TODO RECEIVE
  66. class Receive implements Runnable
  67. {
  68.         private DatagramSocket ds;
  69.         Receive(DatagramSocket ds)
  70.         {
  71.                 this.ds = ds;
  72.         }
  73.         public void run()
  74.         {
  75.                 try
  76.                 {
  77.                         while(true)
  78.                         {
  79.                                 //System.out.println("jieshou");
  80.                                 byte[] buf = new byte[1024*64];
  81.                                 DatagramPacket dp = new DatagramPacket(buf,buf.length);
  82.                                 ds.receive(dp);
  83.                                 String ip = dp.getAddress().getHostAddress();
  84.                                 int port = dp.getPort();
  85.                                 String data = new String(dp.getData(),0,dp.getLength());
  86.                                 System.out.println(ip+"-"+port+"土豆:"+data);
  87.                         }
  88.                 }
  89.                 catch(IOException e)
  90.                 {
  91.                         throw new RuntimeException("接收失败");
  92.                 }
  93.                
  94.         }
  95. }
复制代码


第二个客户端:

  1. import java.io.BufferedReader;
  2. import java.io.IOException;
  3. import java.io.InputStreamReader;
  4. import java.net.DatagramPacket;
  5. import java.net.DatagramSocket;
  6. import java.net.InetAddress;
  7. import java.net.SocketException;
  8. public class UdpTwo {

  9.         public static void main(String[] args) {
  10.                 // TODO Auto-generated method stub
  11.                 try
  12.                 {
  13.                         //DatagramSocket send = new DatagramSocket();
  14.                         DatagramSocket receive = new DatagramSocket(10001);
  15.                         new Thread(new OhterSend(receive)).start();
  16.                         new Thread(new OhterReceive(receive)).start();
  17.                 }
  18.                 catch (SocketException se)
  19.                 {
  20.                         throw new RuntimeException("建立数据包套接字失败");
  21.                 }
  22.                
  23.         }

  24. }

  25. //TODO Send
  26. class OhterSend implements Runnable
  27. {
  28.         private DatagramSocket ds;
  29.         OhterSend(DatagramSocket ds)
  30.         {
  31.                 this.ds = ds;
  32.         }
  33.         public void run()
  34.         {
  35.                 try
  36.                 {
  37.                         BufferedReader bfr = new BufferedReader(new InputStreamReader(System.in));
  38.                         String line = null;
  39.                         while ((line=bfr.readLine())!=null)
  40.                         {
  41.                                 if(line.equals("over"))
  42.                                         break;
  43.                                 byte[] buf = line.getBytes();
  44.                                 DatagramPacket dp = new DatagramPacket(buf,buf.length,
  45.                                                 InetAddress.getByName("192.168.2.105"),10002);
  46.                                 ds.send(dp);
  47.                                 //System.out.println(line.toUpperCase());
  48.                         }
  49.                         bfr.close();
  50.                         ds.close();
  51.                 }
  52.                 catch (IOException e)
  53.                 {
  54.                         throw new RuntimeException("发送失败");
  55.                 }
  56.         }
  57. }

  58. //TODO RECEIVE
  59. class OhterReceive implements Runnable
  60. {
  61.         private DatagramSocket ds;
  62.         OhterReceive(DatagramSocket ds)
  63.         {
  64.                 this.ds = ds;
  65.         }
  66.         public void run()
  67.         {
  68.                 try
  69.                 {
  70.                         while(true)
  71.                         {
  72.                                 //System.out.println("jieshou");
  73.                                 byte[] buf = new byte[1024];
  74.                                 DatagramPacket dp = new DatagramPacket(buf,buf.length);
  75.                                 ds.receive(dp);
  76.                                 String ip = dp.getAddress().getHostAddress();
  77.                                 int port = dp.getPort();
  78.                                 String data = new String(dp.getData(),0,dp.getLength());
  79.                                 System.out.println(ip+"-"+port+"番茄:"+data);
  80.                         }
  81.                 }
  82.                 catch(IOException e)
  83.                 {
  84.                         throw new RuntimeException("接收失败");
  85.                 }
  86.                
  87.         }
  88. }
复制代码

作者: RuntimeError!    时间: 2013-11-26 11:19
本帖最后由 RuntimeError! 于 2013-11-26 11:21 编辑

可以实现的 毕老师内个我试过- -
你开两个命令行 分别执行两个class
  eclipse的也有两个命令行
楼主出什么问题了啊
作者: 王雷1    时间: 2013-11-26 11:41
TCP我的代码没有问题,但是UDP我上面的代码没有成功实现,能不能给看看问题出在哪?
作者: 一直很安静    时间: 2013-11-26 14:17
freehiker 发表于 2013-11-26 11:12
我把我写的练习发上来吧,可以实现同一台电脑聊天,但是需要通过cmd命令执行class,不能通过eclipse
第一个 ...

我想是可以通过eclipse的,我之前也遇到过,不知道你和我的问题一样不一样,用eclipse没办法开启两个控制台,这种情况只要将两个程序放在不同的工作间就可以了,这样可以开两个eclipse
作者: freehiker    时间: 2013-11-26 14:18
一直很安静 发表于 2013-11-26 14:17
我想是可以通过eclipse的,我之前也遇到过,不知道你和我的问题一样不一样,用eclipse没办法开启两个控制 ...

恩,只是那样太占资源了,所以我就用dos了,省力,方便,直观
作者: 一直很安静    时间: 2013-11-26 14:20
freehiker 发表于 2013-11-26 14:18
恩,只是那样太占资源了,所以我就用dos了,省力,方便,直观

嗯,开一个eclipse就挺占内存的,只是我当时是基础测试的一个题,要求必须要使用eclipse
作者: freehiker    时间: 2013-11-26 14:27
一直很安静 发表于 2013-11-26 14:20
嗯,开一个eclipse就挺占内存的,只是我当时是基础测试的一个题,要求必须要使用eclipse ...

你不需要在两个workspace下操作的,同一个包下都没问题的,在右下角有一个Display selected Console,可以切换
作者: 一直很安静    时间: 2013-11-26 14:29
freehiker 发表于 2013-11-26 14:27
你不需要在两个workspace下操作的,同一个包下都没问题的,在右下角有一个Display selected Console,可 ...

哦哦 谢谢拉
作者: freehiker    时间: 2013-11-26 14:30
一直很安静 发表于 2013-11-26 14:29
哦哦 谢谢拉

不客气:lol




欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2