| | |
Problem with Client-Server Socket Connection
![]() |
•
•
Join Date: Jan 2006
Posts: 24
Reputation:
Solved Threads: 0
I was creating a chat application in Java Swing/Socket Programming. The same when I created in non GUI app. it worked http://www.daniweb.com/code/snippet448.html . But in GUI, they can't connect now. I have tried it thru many ways but still m getting errors for connection.
Can anyone can help me......
/*********************
ChatServer.java
**********************/
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
import java.net.*;
public class ChatServer extends JFrame implements ActionListener
{
JPanel jpA = new JPanel();
JTextArea txtA = new JTextArea(10,10);
JTextArea txtB = new JTextArea(10,10);
JButton send = new JButton("Send");
JTextField status = new JTextField(20);
int PORT = 8145;
InputStream inStream;
DataInputStream inDataStream;
OutputStream outStream;
DataOutputStream outDataStream;
String message = "";
ServerSocket sock = null;
Socket conn = null;
public ChatServer(String title){
setTitle(title);
Container c = this.getContentPane();
send.addActionListener(this);
jpA.add(txtA);
jpA.add(send);
jpA.add(txtB);
jpA.add(status);
c.add(jpA);
}
public void actionPerformed(ActionEvent ae){
if(ae.getSource()==send){
try{
InetAddress HOST = InetAddress.getLocalHost();
Socket sock = new Socket(HOST,PORT);
outStream = conn.getOutputStream();
outDataStream = new DataOutputStream (outStream);
message = txtA.getText();
outDataStream.writeUTF(message);
status.setText("Message have been sent");
}catch(Exception e){status.setText(message);}
}
}
public void connectClient(){
try{
sock = new ServerSocket(PORT);
sock.accept();
message = "Client Connected";
System.out.println(message);
}catch(Exception e){status.setText(message);System.out.println("Connection Problem..."+e);}
status.setText(message);
}
public static void main(String args[]){
ChatServer cs = new ChatServer("This is the Server");
cs.setVisible(true);
cs.setSize(400,350);
cs.connectClient();
}
}
/*********************
ChatClient.java
**********************/
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
import java.net.*;
public class ChatClient extends JFrame implements ActionListener
{
JPanel jpA = new JPanel();
JTextField tfa = new JTextField(15);
JTextField tfb = new JTextField(5);
JButton connect = new JButton("Connect");
JTextArea txtA = new JTextArea(10,10);
JTextArea txtB = new JTextArea(10,10);
JButton send = new JButton("Send");
JButton recieve = new JButton("Recieve");
JTextField status = new JTextField(20);
int PORT = 8145;
String HOST = "";
InputStream inStream;
DataInputStream inDataStream;
OutputStream outStream;
DataOutputStream outDataStream;
String message = "";
Socket sock = null;
public ChatClient(String title){
setTitle(title);
Container c = this.getContentPane();
connect.addActionListener(this);
send.addActionListener(this);
recieve.addActionListener(this);
jpA.add(tfa);
jpA.add(connect);
jpA.add(txtA);
jpA.add(send);
jpA.add(recieve);
jpA.add(txtB);
jpA.add(status);
c.add(jpA);
}
public void actionPerformed(ActionEvent ae){
if(ae.getSource()==connect){
status.setText("Connecting.......");
HOST = tfa.getText();
try{
sock = new Socket(HOST,PORT);
inStream = sock.getInputStream ();
inDataStream = new DataInputStream ( inStream );
message = inDataStream.readUTF();
status.setText("You are connected");
}catch(Exception e){status.setText(message);}
}
if(ae.getSource()==recieve){
try{
System.out.println("sock details: "+sock);
inStream = sock.getInputStream ();
System.out.println("sock inoutstream details: "+inStream);
inDataStream = new DataInputStream ( inStream );
message = inDataStream.readUTF();
System.out.println("message is that : "+message);
if(message==null)
txtB.setText("No message was recieved");
else
txtB.setText(message);
}catch(Exception e){status.setText(""+e);}
}
}public static void main(String args[]){
ChatClient cc = new ChatClient("This is the Client");
cc.setVisible(true);
cc.setSize(400,350);
}
}
Can anyone can help me......
/*********************
ChatServer.java
**********************/
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
import java.net.*;
public class ChatServer extends JFrame implements ActionListener
{
JPanel jpA = new JPanel();
JTextArea txtA = new JTextArea(10,10);
JTextArea txtB = new JTextArea(10,10);
JButton send = new JButton("Send");
JTextField status = new JTextField(20);
int PORT = 8145;
InputStream inStream;
DataInputStream inDataStream;
OutputStream outStream;
DataOutputStream outDataStream;
String message = "";
ServerSocket sock = null;
Socket conn = null;
public ChatServer(String title){
setTitle(title);
Container c = this.getContentPane();
send.addActionListener(this);
jpA.add(txtA);
jpA.add(send);
jpA.add(txtB);
jpA.add(status);
c.add(jpA);
}
public void actionPerformed(ActionEvent ae){
if(ae.getSource()==send){
try{
InetAddress HOST = InetAddress.getLocalHost();
Socket sock = new Socket(HOST,PORT);
outStream = conn.getOutputStream();
outDataStream = new DataOutputStream (outStream);
message = txtA.getText();
outDataStream.writeUTF(message);
status.setText("Message have been sent");
}catch(Exception e){status.setText(message);}
}
}
public void connectClient(){
try{
sock = new ServerSocket(PORT);
sock.accept();
message = "Client Connected";
System.out.println(message);
}catch(Exception e){status.setText(message);System.out.println("Connection Problem..."+e);}
status.setText(message);
}
public static void main(String args[]){
ChatServer cs = new ChatServer("This is the Server");
cs.setVisible(true);
cs.setSize(400,350);
cs.connectClient();
}
}
/*********************
ChatClient.java
**********************/
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
import java.net.*;
public class ChatClient extends JFrame implements ActionListener
{
JPanel jpA = new JPanel();
JTextField tfa = new JTextField(15);
JTextField tfb = new JTextField(5);
JButton connect = new JButton("Connect");
JTextArea txtA = new JTextArea(10,10);
JTextArea txtB = new JTextArea(10,10);
JButton send = new JButton("Send");
JButton recieve = new JButton("Recieve");
JTextField status = new JTextField(20);
int PORT = 8145;
String HOST = "";
InputStream inStream;
DataInputStream inDataStream;
OutputStream outStream;
DataOutputStream outDataStream;
String message = "";
Socket sock = null;
public ChatClient(String title){
setTitle(title);
Container c = this.getContentPane();
connect.addActionListener(this);
send.addActionListener(this);
recieve.addActionListener(this);
jpA.add(tfa);
jpA.add(connect);
jpA.add(txtA);
jpA.add(send);
jpA.add(recieve);
jpA.add(txtB);
jpA.add(status);
c.add(jpA);
}
public void actionPerformed(ActionEvent ae){
if(ae.getSource()==connect){
status.setText("Connecting.......");
HOST = tfa.getText();
try{
sock = new Socket(HOST,PORT);
inStream = sock.getInputStream ();
inDataStream = new DataInputStream ( inStream );
message = inDataStream.readUTF();
status.setText("You are connected");
}catch(Exception e){status.setText(message);}
}
if(ae.getSource()==recieve){
try{
System.out.println("sock details: "+sock);
inStream = sock.getInputStream ();
System.out.println("sock inoutstream details: "+inStream);
inDataStream = new DataInputStream ( inStream );
message = inDataStream.readUTF();
System.out.println("message is that : "+message);
if(message==null)
txtB.setText("No message was recieved");
else
txtB.setText(message);
}catch(Exception e){status.setText(""+e);}
}
}public static void main(String args[]){
ChatClient cc = new ChatClient("This is the Client");
cc.setVisible(true);
cc.setSize(400,350);
}
}
Not quite sure what it's doing, but looks to me like the he's trying to receive something by pushing a button. Anything sent over the network at any time except that it's available to the client just when the button is pushed is simply thrown away (or that's what it looks like).
There doesn't seem to be any code to handle sending data at all...
Nothing listening for network traffic, nothing listening for things to send.
That connection might be made but never do anything really useful.
There doesn't seem to be any code to handle sending data at all...
Nothing listening for network traffic, nothing listening for things to send.
That connection might be made but never do anything really useful.
As people are clearly allowed to attack me but I'm not allowed to defend myself, I no longer post to this site.
•
•
Join Date: Jun 2004
Posts: 2,108
Reputation:
Solved Threads: 18
I didn't even look too much at the code
I didn't see any multithreading in it, so I just guessed that was the problem. I created one like what I think he's trying to do with swing and it just locked up. It worked via console, but not swing. It was obviously a multithreading problem, but I just gave up on it
I didn't see any multithreading in it, so I just guessed that was the problem. I created one like what I think he's trying to do with swing and it just locked up. It worked via console, but not swing. It was obviously a multithreading problem, but I just gave up on it
![]() |
Similar Threads
- Java Client/Server (Java)
- client server communication problem in CSocket program (C)
- Client Server Applications (Pascal and Delphi)
- How to send files through a socket connection? (C)
- java client server quiz. interesting!!! (Java)
Other Threads in the Java Forum
- Previous Thread: Help Me*******
- Next Thread: Problem with Java Swing
| Thread Tools | Search this Thread |
-xlint add android api applet application applications array arrays automation bank bi binary blackberry bluetooth chat class clear client code compile compiler component database development dice digit eclipse equation error event formatingtextintooltipjava fractal freeze functiontesting game gameprogramming givemetehcodez graphics gui health html hyper ide idea image infinite int integer j2me java javame javaprojects jetbrains jni jpanel jtable julia learningresources linux list login main map method methods mobile myregfun netbeans nonstatic notdisplaying openjavafx pearl problem program project qt recursion scanner screen scrollbar server set sms sort sorting spamblocker sql sqlserver storm string superclass swing system thread threads tree variablebinding windows xor






