2 m0 37047065 m0_37047065 于 2017.01.10 17:58 提问

利用Java的socket编程,实现本地聊天(只模拟发送数字)

源码如下:

package myqq;

import java.io.BufferedInputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Scanner;

public class Chat {
public static void main(String[] args){

    SendMessage sendMessage = new SendMessage();
    ReceiptMessage receiptMessage = new ReceiptMessage();
    sendMessage.start();
    receiptMessage.start();
}

}

class SendMessage extends Thread{

public void run() {
    System.out.println("发送线程已启动!");
    try{
        Socket c = new Socket("127.0.0.1", 9000);
        Scanner input = new Scanner(System.in);
        OutputStream os = c.getOutputStream();
        int msg = 0;            
        while(msg!=886){
            msg = input.nextInt();
            os.write(msg);
        }
        os.close();
        c.close();
    }catch (Exception e) {
        System.out.println("服务器异常,停止服务");
        e.printStackTrace();
    }
}   

}

class ReceiptMessage extends Thread{

public void run() {
    System.out.println("接收线程已启动!");
    try{
        ServerSocket ss = new ServerSocket(9000);
        System.out.println("服务器端开始监听9000端口");
        //获取输入流
        Socket s = null;
        InputStream is = new BufferedInputStream(null);
        int msg = 0;
        do{
            s = ss.accept();
            is = s.getInputStream();
            msg = is.read();
            System.out.println(msg);    
        }
        while(msg!=886);
        is.close();
        s.close();

    }catch (Exception e) {
        System.out.println("服务器异常,停止服务");
        e.printStackTrace();
    }
}

}

问题:编译能通过,执行时报错图片说明
这个什么问题啊?

2个回答

zhangpan_soft
zhangpan_soft   2017.01.10 19:40

把之前的class删除掉,只留下.java文件,然后编译的时候带包名编译,就ok了,运行的时候,带包名运行,问题解决!带包编译我忘了命令了,你可以百度下带包编译!

baluzju
baluzju   2017.01.10 21:08

方法1:
删掉Chat.java文件中的 package myqq;
然后再次
javac Chat.java
java Chat

方法2:
不改源文件
d:\ Java md d:\myqq
d:\ Java mv d:\Java\Chat.class d:\Java\myqq\Chat.class
d:\ Java java myqq.Chat

Csdn user default icon
上传中...
上传图片
插入图片