• Post Reply Bookmark Topic Watch Topic
  • New Topic

server client relationship is not good! :(  RSS feed

 
parthiban rajendran
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hello friends,
i have been studying networking in headfirst java. i tried the client side chat program given in the book. just take a look at both the client and server programs. then i will tell the problem.


CLIENT program:
import java.io.*;
import java.net.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;


class Client
{

String serverAddress = "10.145.6.56";
Socket clientSocket;
PrintWriter writer;


JFrame frame;
JPanel mainPanel;
JTextField messageArea;
JButton sendButton;


public void go()
{

buildGUI();
setUpNetwork();
closeNetwork();

}


public void buildGUI()
{

frame = new JFrame("Parthi's client side");
frame.setSize(400,200);
frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE);


mainPanel = new JPanel();


messageArea = new JTextField(20);
mainPanel.add(messageArea);


sendButton = new JButton("Send to Server");
mainPanel.add(sendButton);


MySendListener sendListener = new MySendListener();
sendButton.addActionListener(sendListener);


frame.getContentPane().add(BorderLayout.CENTER,mainPanel);
frame.setVisible(true);

}



public void setUpNetwork()
{
try

{
clientSocket = new Socket(serverAddress,5000);


writer = new PrintWriter(clientSocket.getOutputStream());
writer.println("hello server.Do you hear me?");

}
catch(Exception ex)
{
System.out.println("client: i tried to connect with server. i could not!");
}
}


public void closeNetwork()
{
writer.close();
}



//-------- INNER CLASSES -----------------------------------------------
class MySendListener implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
try
{
System.out.println("sending to server: "+messageArea.getText());

writer.println("client says: "+messageArea.getText());
writer.flush();
}
catch(Exception ex)
{
ex.printStackTrace();
}
messageArea.setText("");
messageArea.requestFocus();

}
}
//--------- end of INNER CLASSES ---------------------------------------



}



public class testClient2
{

public static void main(String args[])
{
Client kabali = new Client();
kabali.go();
}

}


SERVER program:

import java.io.*;
import java.net.*;
import java.awt.*;
import javax.swing.*;


class Server
{

public void go()
{

try
{

ServerSocket server = new ServerSocket(5000);


while (true)
{


Socket sock = server.accept();
InputStreamReader inputStream = new InputStreamReader(sock.getInputStream());
BufferedReader reader = new BufferedReader(inputStream);

String line = reader.readLine();

System.out.println("client kabali says: "+line);

reader.close();
}


}
catch(Exception ex)
{
ex.printStackTrace();
}

}

}



public class testServer2
{

public static void main(String args[])
{
Server anniyan = new Server();
anniyan.go();
}

}

note: change the IP address of the server according to your system.

both compile and run. initially client says "do you hear me?" and server displays it. but, for any text entered in text area (client side) when i clik the "send to server", the server is not responding. what is the problem in server program? please help me, friends.
 
Joe Ess
Bartender
Posts: 9406
12
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You close your writer in closeNetwork(). After that point you can't use it. Put a System.out message in that method and see if it's called before you press your "send" button.
 
Sowjanya Chowdary
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes Parthiban,
In client class's go method, you are calling three methods one after other.

buildGUI();
setUpNetwork();
closeNetwork();


So the network connection is closed immediately after "setUpNetwork()".
and you can't write anything to the server.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!