错误在哪里//为什么不起作用? (Java聊天程序)

时间:2013-08-07 22:20:32

标签: java udp chat

我想写一个UDP java聊天程序,我可以使用这个程序向另一个人发送和接收消息。我想通过自己的插件编程(谷歌搜索等),所以我不完全理解每个部分。基本思路是将一个想要与之聊天的随机IP作为字符串读取,将其转换为IP并启动两个线程,一个用于从端口A发送消息,另一个用于在端口B接收消息(线程用于存在两者都能够同时发送和接收消息)。每个线程都有自己的类。到现在为止还挺好。现在这两个类都有运行的方法,它们都在一个大的try-catch块中。在两个catch块中,我添加了几条错误消息,首先是“Test123”,然后是“Test456”,所以我可以理解当发生时会发生什么。编译代码时,我可以输入IP(我试过localhost进行测试)。但是当我输入消息时,我应该收到消息“聊天伙伴发送:”,但我没有得到任何东西。现在两个线程都处于无限循环中,因此当我强制程序终止时(通过按Ctrl + C(我运行.class via命令)),我在程序终止之前收到错误消息“Test123”。我的问题是:为什么我没有收到任何消息,为什么程序在强制程序终止时抛出“Test123”?我的错误在哪里? 在此先感谢您的帮助。这是代码:

import java.net.*;
import java.util.Scanner;

public class chat {
    static InetAddress IP;
    static int sPort=11111;
    static int rPort=11112;
    public static void main(String[] args) throws Exception{
        System.out.println("Zu welcher IP soll verbunden werden?");//"which IP do you want to connect with?"
        Scanner sc = new Scanner(System.in);
        String IPraw=sc.next(); //type in the IP address as String
        IP=InetAddress.getByName(IPraw); //converting the String into real IP address
        Thread sender = new sender();
        sender.start(); //start the sending thread
        Thread receiver = new receiver();
        receiver.start(); //start the receiving thread
    }
}
class sender extends Thread{
    public void run(){
        byte[] sendData = new byte[1024];
        Scanner scantext = new Scanner(System.in);
        try{
            DatagramSocket Socket = new DatagramSocket();
            while(true){
                String TextSend = scantext.next();
                sendData = TextSend.getBytes();
                DatagramPacket out = new DatagramPacket(sendData, sendData.length, chat.IP, chat.rPort);
                Socket.send(out);
            }
        }
        catch(Exception e){
        System.out.println("Test123");
        }
    }
}

class receiver extends Thread{
    public void run(){
        byte[] receiveData = new byte[1024];
        try{
            DatagramSocket socket = new DatagramSocket();
            while(true){
                DatagramPacket in = new DatagramPacket(receiveData, receiveData.length, chat.IP, chat.sPort);
                socket.receive(in);
                String message = new String(in.getData());
                System.out.println("Chatpartner sagt: " + message);//"partner said <message>"           
            }
        }
        catch(Exception e){
        System.out.println("Test456");
        }
    }
}

3 个答案:

答案 0 :(得分:1)

发件人线程正在等待您在System.in上插入一些数据(只需输入内容)。 它在这里被阻止,在这一行:String TextSend = scantext.next();

答案 1 :(得分:0)

您的应用程序似乎无法正常运行,因为您要将数据发送到不同的端口。

static int sPort=11111;
static int rPort=11112;

您需要将其发送到同一个端口(如果要将其发送到您自己的计算机,则需要指定local ip-addresslocalhost127.0.0.1。< / p>

至于你的第一个问题

Why don't I receive any message

您是否正在连接127.0.0.1?你的参数究竟是什么?

至于你的第二个问题

why does the program throw "Test123" when I force the program to terminate?

因为在那一刻你打破了while循环

try{
    DatagramSocket Socket = new DatagramSocket();
    while(true){
        String TextSend = scantext.next();
        sendData = TextSend.getBytes();
        DatagramPacket out = new DatagramPacket(sendData, sendData.length, chat.IP, chat.rPort);
        Socket.send(out);
    }
}

终止这样的应用程序会导致异常。

答案 2 :(得分:0)

您正在发送一个端口,并在另一个端口上接收。如果您希望此代码发送和接收自己的消息,那么这些端口将需要相同。如果您打算与其他人实际聊天(即使您自己在本地主机上聊天),则必须在您的发送端口上接收另一个聊天并发送您的接收端口。

另外,hexafraction说:

  

不要捕获异常并打印下一个无用的消息。请改用e.printStackTrace()。

这就是你的错误发生的地方,你抓住了它并打印了你的消息