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:


Please help me in this.
The program waits for a connection on TCP port 3000 in
Laccept.Since this is called in
actionPerformedit 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:
Obs:
invokeLateris used to force the code to run in the EDT. The documentation says: