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

  • Home
  • SEARCH
  • 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 4116024
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T22:39:25+00:00 2026-05-20T22:39:25+00:00

Note-I have tried to make my pc both server and client. I tried a

  • 0

Note-I have tried to make my pc both server and client.

I tried a lot but cannot understand the reason my program hangs.

Whenever i click connect,program hangs.

Client Side

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.net.*;
import java.io.*;
 class chatboxClient {
 JFrame fr;
 JPanel p;
 JButton send;
 JTextArea ta;
 JRadioButton rb;
 chatboxServer cbS=new chatboxServer();

 chatboxClient() {

 fr=new JFrame("ChatBox_CLIENT");
 p=new JPanel();
 send=new JButton("send");
 send.addActionListener(new ActionListener() {       // action listener for send
  public void actionPerformed(ActionEvent ae) {
    sendActionPerformed(ae);
  }
 });
 ta=new JTextArea();
 ta.setRows(20);
 ta.setColumns(20);
 rb=new JRadioButton("Connect");               // action listener for connect
 rb.addActionListener(new ActionListener() {
 public void actionPerformed(ActionEvent ae) {
  connectActionPerformed(ae); 
  }
 });
 fr.add(p);
 p.add(ta);
 p.add(rb);
 p.add(send);
 fr.setSize(500,500);
 fr.setResizable(false);
 fr.setVisible(true);
}

 public void connectActionPerformed(ActionEvent ae) {
  try {
   cbS.Laccept();
   rb.setEnabled(false);
   JOptionPane.showMessageDialog(new JFrame()," Sockets InterConnected!");
  } catch(Exception exc) {
     JOptionPane.showMessageDialog(new JFrame()," Connection Error..");
  }
  }

 public void sendActionPerformed(ActionEvent ae) {
   try { 
String s=ta.getText();
InetAddress address=InetAddress.getLocalHost();
DatagramSocket ds=new DatagramSocket(3000,address);
byte buffer[]=new byte[800];
buffer=s.getBytes();
DatagramPacket dp=new DatagramPacket(buffer,buffer.length,address,3000);
if(true) {
     ds.send(dp);

     cbS.Receive(s); // call Receive method of chatboxServer class
    }
 }  catch(Exception exc) {
        JOptionPane.showMessageDialog(new JFrame(),"Error sending Message");
  }

}

 public static void main(String args[]) {
  new chatboxClient();
 }
}

SERVER SIDE

import java.awt.*;
import java.net.*;
import javax.swing.*;
import java.awt.event.*;
class chatboxServer {
 JFrame fr;
 JPanel p;
 JTextArea ta;
 JButton send;
 ServerSocket ss;
 byte buffer[]=new byte[800];

chatboxServer() {
fr=new JFrame("ChatBox_SERVER");
p=new JPanel();
ta=new JTextArea();
ta.setRows(20);
ta.setColumns(20);
send=new JButton("send");
fr.add(p);
p.add(ta);
p.add(send);
fr.setVisible(true);
fr.setSize(500,500);
fr.setResizable(false);

}

public void Receive(String sm) {
 try {
 buffer=sm.getBytes();
 InetAddress address=InetAddress.getLocalHost();
 DatagramSocket ds=new DatagramSocket(3000,address);
 DatagramPacket dp=new DatagramPacket(buffer,buffer.length);
 ds.receive(dp);
 String s=new String(dp.getData(),0,dp.getLength());
 ta.setText(s);  
}  catch(Exception exc) {
    System.out.println("Error Receiving..");
   }

}

  public void Laccept() {
    try {
      ss=new ServerSocket(3000);     // First making port number 3000 on server to listen
      Socket s=ss.accept();
    } catch(Exception exc) {
          JOptionPane.showMessageDialog(new JFrame(),"Accept Failed :3000 :Server Side");
      }  
  }
 } 

The part that i think is causing a problem is when i call to Laccept().

The output:
enter image description here

enter image description here

Please help me in this.

  • 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-20T22:39:26+00:00Added an answer on May 20, 2026 at 10:39 pm

    The program waits for a connection on TCP port 3000 in Laccept.
    Since this is called in actionPerformed it blocks the Event Dispatch Thread (EDT). This is the Thread responsible for managing GUI events and updating the screen. When the EDT is blocked, the GUI will be blocked – frames will not be updated, no reaction on input, … the application hangs!
    You must run such code in another Thread to avoid blocking the EDT: see Concurrency in Swing

    Nowhere in the posted code a TCP connection to port 3000 is being opened!
    Seams like you are mixing TCP (Socket, ServerSocket) and UDP (DatagramSocket).
    See Networking Basics, All About Sockets and All About Datagrams as a start.

    EDIT:
    Basic idea:

    // run outside the EDT
    thread = new Thread(new Runnable() {
        @Override 
        public void run() { 
            Laccept();
        } 
    }); 
    thread.start();
    

    Obs: invokeLater is used to force the code to run in the EDT. The documentation says:

    Causes doRun.run() to be executed asynchronously on the AWT event dispatching thread.

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

Sidebar

Related Questions

I want to make my pc both server and client. This is my code
(Note: This is for MySQL's SQL, not SQL Server.) I have a database column
I have this website, http://www.escuelita.info . Now I tried many times to make it
Note: I've tried searching on google, but couldn't find what I was looking for.
We have the go button enabled but somehow we haven't been able to make
Note - I have not delved very deeply into Apple's iPhone SDK yet. However,
I have the following HTML (note the CSS making the background black and text
I have to give a general note to some huge Java project for which
Note that this function does not have a { and } body. Just a
Note: I am just consuming webservice I have no control over webservice code. So

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.