• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

server client relationship is not good! :(

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 9626
16
Mac OS X Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic