Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6995973
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T20:05:53+00:00 2026-05-27T20:05:53+00:00

**Here is a simple code of chat between two peers. According to me, the

  • 0

**Here is a simple code of chat between two peers. According to me, the code does what it should have but I am facing difficulty solving this SocketException error.
This code worked fine till i made the last modifications. And now I am finding it difficult to trace the error.

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;
import java.util.*;
import java.net.*;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;

public class UDPchat extends Thread implements ActionListener
{
    JFrame f;
    JPanel p;
    JTextField text;
    JButton b;
    JTextArea ta;
    private final static String newline = "\n";
    private final static BufferedReader in = new BufferedReader(new InputStreamReader(System.in));

    int porttor=7777; // port to send/receive datagrams on
    //int porttos=5555;
    String remoteIPaddress= ("127.0.0.1");      // IP to send datagrams

    public UDPchat() throws Exception
    {
        start();      // start thread to receive and display datagrams
        f=new JFrame("CHAT WINDOW");
        p= new JPanel();
        text = new JTextField();
        text.setColumns(25);

        b = new JButton("SEND");

        b.addActionListener(this);

        ta = new JTextArea("Chat messages",20,50);

        ta.setEditable(false);

        f.setLayout(new FlowLayout());

        p.add(text,BorderLayout.NORTH);

        p.add(b,BorderLayout.SOUTH);

        f.setLayout(new BorderLayout());

        f.add(ta,BorderLayout.NORTH);

        f.add(p,BorderLayout.SOUTH);

        f.setSize(400, 400);

        f.setVisible(true);
    }

    @Override

    public void actionPerformed(ActionEvent e)
    {

        try
        {

            String s = text.getText();  // read a String

            text.setText(" ");

            ta.append(newline);

            ta.append(s);

            //System.out.println("Sending to " + remoteIPaddress + " socket " + port + " data: " + s);

            byte[] data = s.getBytes();  // convert to byte array

            DatagramSocket theSocket = new DatagramSocket();  // create datagram socket and the datagram

            DatagramPacket   theOutput = new DatagramPacket(data, data.length, InetAddress.getLocalHost(), 5555);

            theSocket.send(theOutput);     

        }

        catch(Exception et)
        {

            System.out.println("Error sending datagram" + et);

        }

    }

    // thread run method, receives datagram and display contents as a string
    public void run()                
    {

        try
        {

            // open DatagramSocket to receive 

            DatagramSocket ds = new DatagramSocket(7777);

            // loop forever reading datagrams from the DatagramSocket

            while (true)
            {

                byte[] buffer = new byte[65507];  // array to put datagrams in

                DatagramPacket dp = new DatagramPacket(buffer, buffer.length);  // DatagramPacket to hold the datagram

                try {

                    ds.receive(dp);

                } catch (IOException e) {

                    // TODO Auto-generated catch block

                    System.err.println("chat error2 " + e);

                }   // wait for next datagram

                String s = new String(dp.getData(),0,dp.getLength()); // get contents as a String

                System.out.println("UDP datagram length " + s.length()+ "  from IP " + dp.getAddress() + " received: " + s );

                ta.append(newline);

                ta.append(s);

            }

        }

        catch (SocketException se)
        {

             System.err.println("chat error1 " + se);

        }

        //catch (IOException se) {System.err.println("chat error2 " + se);}

        //System.exit(1);   // exit on error

    }

    public static void main(String args[]) throws Exception
    {

        UDPchat c=new UDPchat();

    }

}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T20:05:53+00:00Added an answer on May 27, 2026 at 8:05 pm

    Check this you making two types of DatagramSockets in your code, try to provide both the same 7777 port. One in your actionPerformed() Method as :

    DatagramSocket theSocket = new DatagramSocket();  // create datagram socket and the datagram
    

    and the one in your run Method as :

    // open DatagramSocket to receive 
    
    DatagramSocket ds = new DatagramSocket(7777);
    

    The Upper constructor is creating a datagram socket and binding it to any available port on the local host machine, and the second one is trying to reach port 7777. Try to initialize both by same port number, that will sort things out for you.

    Hope that might help in some way.

    Regards

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

enter code here Hi All, I have a simple windows service application that connects
I have -- what I think -- is a simple question. Here's my code:
Here is a simple code snippet and I cannot figure out why does it
Here is a simple code to animate webview screen. But the problem is, the
I have this simple code here, nothing too advanced. $(input#send).submit(function(event){ event.preventDefault(); $.ajax({ type: 'POST',
Here is some simple code: DIR* pd = opendir(xxxx); struct dirent *cur; while (cur
Here is a simple code that shows what I think is a bug when
So here is the simple code: [System.ComponentModel.DefaultValue(true)] public bool AnyValue { get; set; }
I've got a quite strange problem here. I'm calling some simple code via Ajax.Updater:
Here is a simple piece of code: import java.io.*; public class Read { public

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.