24小时热门版块排行榜    

Znn3bq.jpeg
北京石油化工学院2026年研究生招生接收调剂公告
查看: 800  |  回复: 1

wyw20123wyw

新虫 (初入文坛)

[求助] JAVA socket 通信时接收不到数据(和别的语言编写的socket程序通信)

我在网上下载了一个java编写的服务器端和客户端程序,服务器端和客户端可以实现数据的收发,但是我只开启服务器端,与别的客户端(如C#、C++build编写)通信时,建立l连接后,可以发送数据,但是接收不到;或者反过来,启动别的语言编写的服务器端、java编写的客户端,也是能发不能收,不知道问题会出在哪,求高手指点。
界面代码:
CODE:
package nupt.java.socket;
import java.awt.*;
import java.awt.event.*;
//import java.io.*;
public class Face extends Frame {
       
      private static final long serialVersionUID = 1L;
    Button clientBtn, serverBtn;
    TextArea ta;
    TextField tfaddress, tfport, tftype;
    Label lbl1,lbl2,lbl3;
    int port;
    Client client;
    Server server;
    boolean iamserver;
    static Face frm;
    public Face() {
          // 实例化组件
        clientBtn = new Button("客户端");
        serverBtn = new Button("服务器");
        ta = new TextArea("", 10, 50, TextArea.SCROLLBARS_BOTH);
        lbl1 = new Label("IP地址:");
        tfaddress = new TextField("172.16.57.156", 10);
        lbl2 = new Label("端口:");
        tfport = new TextField("8080");
        lbl3 = new Label("发送内容:");
        tftype = new TextField(40);
        tftype.addKeyListener(new TFListener());
        ta.setEditable(false);
        //向容器中加入以上组件
        setLayout(new   FlowLayout());
        add(lbl1);
        add(tfaddress);
        add(lbl2);
        add(tfport);
        add(clientBtn);
        add(serverBtn);
        add(ta);
        add(lbl3);
        add(tftype);
        //设置格式
        setLocation(400, 250);                //窗口显示再屏幕的位置坐标
        setSize(400, 300);                      //设置窗体大小
        setTitle("基于Socket和多线程编程的通信程序");
        this.setVisible(true);                   //设置窗体可见
        //事件响应
        clientBtn.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                port = Integer.parseInt(tfport.getText());
                client = new Client(tfaddress.getText(), port, frm);
                client.start();
                tfaddress.setEnabled(false);
                tfport.setEnabled(false);
                serverBtn.setEnabled(false);
                clientBtn.setEnabled(false);
            }
        });
        serverBtn.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                port = Integer.parseInt(tfport.getText());
                server = new Server(port, frm);
                server.start();
                iamserver = true;
                tfaddress.setText("成为服务器");
                tfaddress.setEnabled(false);
                tfport.setEnabled(false);
                serverBtn.setEnabled(false);
                clientBtn.setEnabled(false);
            }
        });
        addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
    }
      public static void main(String[] args) {        //主方法
             // TODO Auto-generated method stub
            
             frm = new Face();
      }
      private class TFListener implements KeyListener {
        public void keyPressed(KeyEvent e) {
          if (e.getKeyCode() == KeyEvent.VK_ENTER) { //按Enter输出显示聊天内容
                ta.append("发送信息: " + tftype.getText() + "\n");
                //ps.println(tftype.getText());
                if (iamserver)
               
                    server.dataout(tftype.getText());
                else
                    client.dataout(tftype.getText());
               
                tftype.setText("");
            }
        }
        public void keyTyped(KeyEvent e) {
        }
        public void keyReleased(KeyEvent e) {
        }
    }
}
服务器端代码:

package nupt.java.socket;
import java.awt.*;
import java.net.*;
import java.io.*;
public class Server extends Thread {
        ServerSocket skt;   // ServerSocket类监听进入的连接,为每个新的连接产生一个Socket对象      
    Socket Client[ ]=new Socket[10];
    Socket Client1=null;
    int i = 0;
    TextArea in;
    int port,k=0,l=0;
    PrintStream ps;
    Face chat;
    public Server(int port, Face chat) {
        try {
            this.port = port;
            skt = new ServerSocket(port);
            this.chat = chat;
        } catch (IOException e) {
            chat.ta.append(e.toString());
        }
    }
    public void run() {
        chat.ta.append("等待连线......");
        while (true) {
            try {
            Client[k] = skt.accept();//当有客户端连接时就新建一个子线程
         
            if (i < 2) {
              ServerThread server[] = new ServerThread[10];
              server[k]= new ServerThread(Client[k], this.chat, i);
                 l=server.length;
                 server[k].start();
                chat.ta.append("客户端"+ Client[k].getInetAddress() + "已连线\n");
               
                //for(int j=0;j<server.length;j++)
               
                ps = new PrintStream(server[k].getClient().getOutputStream());
                i = server[k].getI();
                k++;
            } else {
                //theOutputStream = new PrintStream(null);
            }
        } catch (SocketException e) {
            } catch (IOException e) {
                chat.ta.append(e.toString());
            }
        }
    }
    public void dataout(String data) {
        //for(int j=0;j<l;j++)
        ps.println(data);
    }
}
class ServerThread extends Thread {
    ServerSocket skt;
    Socket Client;
    TextArea in;
    int port,i;
    BufferedReader theInputStream;
    PrintStream ps;
    String readin;
    Face chat;
//服务端子线程
    public ServerThread(Socket s, Face chat, int i) {
        this.i = ++i;
        Client = s;
        this.chat = chat;
    }
    public int getI() {
        return this.i;
    }
    public Socket getClient() {
        return this.Client;
    }
    public void run() {
          try {
            theInputStream = new BufferedReader(new InputStreamReader(Client
                    .getInputStream()));
            ps = new PrintStream(Client.getOutputStream(),true);
            
            while (true) {
                    readin = theInputStream.readLine();
                     //System.out.println("可以接收吗??");
                chat.ta.append("收到信息: "+readin+"\r\n");
               
      
            }
        } catch (SocketException e) {
            chat.ta.append("连线中断!\n");
                           // 设置组件可用性
            chat.clientBtn.setEnabled(true);
            chat.serverBtn.setEnabled(true);
            chat.tfaddress.setEnabled(true);
            chat.tfport.setEnabled(true);
            try {
                i--;
                skt.close();
                Client.close();
            } catch (IOException err) {
                chat.ta.append(err.toString());
            }
        } catch (IOException e) {
            chat.ta.append(e.toString());
        }
    }
    public void dataout(String data) {
        ps.println(data);
        //ps.flush();
    }
}
客户端代码:

package nupt.java.socket;
import java.net.*;
import java.io.*;

//import javax.swing.Timer;
public class Client extends Thread {
      Socket skt;                                  // 用于客户端的连接
    InetAddress host;                        // 主机地址
    int port;                                     // 端口号
    BufferedReader theInputStream;
    PrintStream ps;
    String readin;
    Face chat;
    public Client(String ip, int p, Face chat) {
        try {
            host = InetAddress.getByName(ip);            // 获取IP地址
            port = p;                                                  // 获取端口号
            this.chat = chat;
        } catch (IOException e) {
            chat.ta.append(e.toString());
        }
    }
    public void run() {
        try {
            chat.ta.append("准备连线,稍后!");
            skt = new Socket(host, port);                     // 新建Socket对象
            chat.ta.append("成功\n");                   // 缓冲区末尾添加字符串
            ps = new PrintStream(skt.getOutputStream(),true);
         
            theInputStream = new BufferedReader(new InputStreamReader(skt.getInputStream()));
         
            while (true) {
                     readin = theInputStream.readLine();
                     //System.out.println("可以接收吗??");
                chat.ta.append("收到信息: "+readin+"\r\n");
              
               
            }
        } catch (SocketException e) {
            chat.ta.append("未连上!\n");
            chat.clientBtn.setEnabled(true);
            chat.serverBtn.setEnabled(true);
            chat.tfaddress.setEnabled(true);
            chat.tfport.setEnabled(true);
           try {
               skt.close();
           }
            catch (IOException e1) {
                chat.ta.append(e1.toString());
            }
        } catch (IOException e) {
            chat.ta.append(e.toString());
        }
    }
    public void dataout(String data) {
        ps.println(data);
        //ps.flush();
    }
}

[ Last edited by jjdg on 2013-11-24 at 02:29 ]
回复此楼

» 猜你喜欢

» 本主题相关价值贴推荐,对您同样有帮助:

已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖

wyw20123wyw

新虫 (初入文坛)

问题解决了,当在C#或者VB客户端输完字符时,敲下回车再发送,客户端就可以收到了;当服务器和客户端在不同的电脑上连接通信时,需要关闭服务器端的防火墙,否则端口不开放,连接不上
2楼2013-12-02 21:12:18
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖
相关版块跳转 我要订阅楼主 wyw20123wyw 的主题更新
最具人气热帖推荐 [查看全部] 作者 回/看 最后发表
[考研] 368化学求调剂 +6 wwwwabcde 2026-04-07 7/350 2026-04-08 19:14 by 我减肥1
[考研] 专硕310求调剂 +6 捞捞我…. 2026-04-04 7/350 2026-04-08 17:02 by kookay8
[考研] 298求调剂 +4 manman511 2026-04-05 4/200 2026-04-08 16:50 by tjzhao
[考研] 一志愿南京航空航天大学 材料与化工329分求调剂 +11 Mr. Z 2026-04-05 12/600 2026-04-08 16:15 by luoyongfeng
[考研] 388求调剂 +6 四川王涛 2026-04-07 8/400 2026-04-08 00:17 by JourneyLucky
[考研] 315求调剂 +17 小羊小羊_ 2026-04-02 18/900 2026-04-07 22:01 by lijunpoly
[考研] 363求调剂 +9 zh096 2026-04-04 9/450 2026-04-07 21:51 by 418490947
[考研] 生物工程求调剂 +13 喜欢还是不甘心 2026-04-05 13/650 2026-04-07 16:55 by Ecowxq666!
[考研] 085100建筑学 寻求跨专业调剂 一志愿南大294分 校级省级国家级奖项若干 踏实肯干 +3 1021075758 2026-04-06 4/200 2026-04-07 09:23 by 蓝云思雨
[考研] 085405软件工程301分求调剂,专硕可跨专业,四六级已过 +3 静静想想 2026-04-05 3/150 2026-04-06 15:23 by nepu_uu
[考研] 一志愿南航,数一英一学硕317求调剂!! +6 Acaciad 2026-04-04 6/300 2026-04-06 12:13 by 考研学校招点人
[考研] 0860 求调剂 一志愿国科大 348 分 +3 WiiiP 2026-04-03 3/150 2026-04-05 17:43 by Ecowxq666!
[考研] 313求调剂 +3 海日海日 2026-04-04 3/150 2026-04-05 07:48 by 544594351
[考研] 调剂 +11 JLLLLLLLLLL 2026-04-03 11/550 2026-04-04 22:21 by hemengdong
[考研] 一志愿上海大学生物学346 +3 上海大学346调剂 2026-04-03 3/150 2026-04-04 20:20 by dongzh2009
[考研] 266求调剂 +8 学员97LZgn 2026-04-03 8/400 2026-04-04 09:02 by 20021109
[考研] 求调剂 +4 15064154688 2026-04-03 5/250 2026-04-03 15:07 by zrongyan
[考研] 求调剂 +3 心想事成可 2026-04-03 3/150 2026-04-03 11:22 by wangjy2002
[考研] 285求调剂 +7 AZMK 2026-04-02 9/450 2026-04-03 11:12 by wanwan00
[考研] 材料求调剂 +10 呢呢妮妮 2026-04-01 13/650 2026-04-02 09:17 by olim
信息提示
请填处理意见