Problem with Client-Server Socket Connection

Reply

Join Date: Jan 2006
Posts: 24
Reputation: Rajnesh is an unknown quantity at this point 
Solved Threads: 0
Rajnesh Rajnesh is offline Offline
Newbie Poster

Problem with Client-Server Socket Connection

 
0
  #1
Jan 11th, 2006
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);
}
}
Reply With Quote Quick reply to this message  
Join Date: Jun 2004
Posts: 2,108
Reputation: server_crash is on a distinguished road 
Solved Threads: 18
server_crash server_crash is offline Offline
Postaholic

Re: Problem with Client-Server Socket Connection

 
0
  #2
Jan 12th, 2006
It doesn't look well multithreaded What's the error? I'm guessing it just locks up and never connects, right?
Reply With Quote Quick reply to this message  
Join Date: Nov 2004
Posts: 6,144
Reputation: jwenting is just really nice jwenting is just really nice jwenting is just really nice jwenting is just really nice 
Solved Threads: 212
Team Colleague
jwenting's Avatar
jwenting jwenting is offline Offline
duckman

Re: Problem with Client-Server Socket Connection

 
0
  #3
Jan 12th, 2006
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.
As people are clearly allowed to attack me but I'm not allowed to defend myself, I no longer post to this site.
Reply With Quote Quick reply to this message  
Join Date: Jun 2004
Posts: 2,108
Reputation: server_crash is on a distinguished road 
Solved Threads: 18
server_crash server_crash is offline Offline
Postaholic

Re: Problem with Client-Server Socket Connection

 
0
  #4
Jan 12th, 2006
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
Reply With Quote Quick reply to this message  
Join Date: Jan 2006
Posts: 24
Reputation: Rajnesh is an unknown quantity at this point 
Solved Threads: 0
Rajnesh Rajnesh is offline Offline
Newbie Poster

Re: Problem with Client-Server Socket Connection

 
0
  #5
Jan 12th, 2006
so could you please tell me where i can implement the Threads or an idea of how to make the Client/Server recieve the buffer contents as soon as the data is sent from the Server/Client
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC