java与c的套接字连接#

hjqgdpho  于 2021-07-06  发布在  Java
关注(0)|答案(0)|浏览(206)

当我尝试用套接字发送或接收消息时,我做不到,在java中,它告诉我连接被拒绝,而在c中,它告诉我端口已被使用;在java中,我通过单击按钮发送消息,然后用线程接收消息,在c中,以类似的方式,如果你能告诉我出了什么问题,我会非常感激的,我已经尝试了所有的方法,但是我找不到答案,谢谢。
java

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
        String mensaje = jTextField1.getText();
        System.out.println(mensaje);
        byte men[] = mensaje.getBytes();
        Socket socket = null;
        ObjectOutputStream out = null;
        try {
            socket = new Socket("127.0.0.1", 7000);
            out = new ObjectOutputStream(socket.getOutputStream());
            out.write(men, 0, men.length);
            out.close();
            socket.close();;
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
class HiloReceptor extends Thread {

        @Override
        public void run() {
            super.run();
            while (true) {
                try {
                    ServerSocket ss = new ServerSocket(7000);
                    Socket socketCanal = ss.accept();
                    ObjectInputStream ois = new ObjectInputStream(socketCanal.getInputStream());
                    byte[] mensaje = new byte[1024];
                    ois.read(mensaje, 0, mensaje.length);
                    String msgRecibido = new String(mensaje, 0 ,mensaje.length);
                    System.out.println(msgRecibido);
                    jLabel1.setText("Mensaje recibido: " + msgRecibido);
                    ois.close();
                    socketCanal.close();
                    ss.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }

    }

c级#

private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                String mensaje = textBox1.Text;
                Console.WriteLine(mensaje);
                byte[] men = UTF8Encoding.UTF8.GetBytes(mensaje);
                IPAddress ip = IPAddress.Parse("127.0.0.1");
                IPEndPoint endp = new IPEndPoint(ip, 7000);
                TcpListener tcpListener = new TcpListener(endp);
                tcpListener.Start();
                //Socket socket = new Socket(ip.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
                Socket socket = tcpListener.AcceptSocket();
                //socket.Connect(endp);

                socket.Send(men, men.Length, 0);

                socket.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error al enviar.\n" + ex);
            }
        }
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        {
            while (!backgroundWorker1.CancellationPending)
            {
                try
                {
                    Console.WriteLine("entre al ciclo");
                    IPAddress ip = IPAddress.Parse("127.0.0.1");
                    IPEndPoint endp = new IPEndPoint(ip, 7000);

                    Socket s = new Socket(ip.AddressFamily, SocketType.Stream, ProtocolType.Tcp);

                    s.Bind(endp);

                    s.Listen(1);

                    Socket canal = s.Accept();

                    byte[] buffer = new byte[1024];

                    msg = "";

                    while (canal.Receive(buffer, buffer.Length, 0) > 0)
                    {
                        msg += Encoding.UTF8.GetString(buffer);
                    }
                    Console.WriteLine(msg);
                    backgroundWorker1.ReportProgress(1);

                    canal.Close();
                    s.Close();
                }
                catch (Exception ex)
                {
                    Console.WriteLine("Error.\n"+ ex);
                }
            }
        }

        private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            if (e.ProgressPercentage == 1)
            {
                label1.Text = "Mensaje recibido: " + msg;
            }
        }

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题