File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Sockets and Internet Protocols and the fly likes What is wrong in this code of Serversocket to client message Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


JavaRanch » Java Forums » Java » Sockets and Internet Protocols
Reply Bookmark "What is wrong in this code of Serversocket to client message" Watch "What is wrong in this code of Serversocket to client message" New topic
Author

What is wrong in this code of Serversocket to client message

sandhiya sindhi
Ranch Hand

Joined: Sep 25, 2003
Posts: 50
hello!
i want to send messages from servr to client for this i have tried such but it is not working.
and also i want that when a person enter login name than it also written in TextArea for this i have also written a code but it is not working too.
kindly help me sir.
import java.io.*;
import java.net.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class NetServer extends JFrame
{
ObjectOutputStream out;
ObjectInputStream in;
Socket client;
ServerSocket server;
String msg;
JTextField msgWrite, login;
JTextArea msgReceived;
JButton sendbtn;
JLabel connected;

NetServer()
{
JLabel label = new JLabel(" ");
JLabel label1= new JLabel(" ");
msgWrite= new JTextField(20);
msgReceived= new JTextArea(15,15);
login= new JTextField(7);
sendbtn= new JButton("Send");
connected = new JLabel(" ");

Container cont= getContentPane();

cont.setLayout(new BoxLayout(cont,BoxLayout.Y_AXIS));

addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowListener we)
{System.exit(0);}
});

cont.add(login);
cont.add(connected);
cont.add(label1);
cont.add(msgReceived);
cont.add(label);
cont.add(msgWrite);
cont.add(sendbtn);
setSize(300,300);
show();
socketConnection();
}
public void actionPerformed(ActionEvent eve)
{
if(eve.getSource()== sendbtn)
{
try
{
out.writeObject(msgWrite.getText());
out.flush();


}
catch(Exception exc)
{
System.out.println("Here the error in action performed method of server side "+exc.getMessage());
}
}
}
public void socketConnection()
{
try
{
server= new ServerSocket(90);
//client= new Socket("l2-03",90);
//System.out.println(client.getInetAddress().getHostName()+"Connected..");
client= server.accept();
connected.setText("Connected");
out= new ObjectOutputStream(client.getOutputStream());
out.flush();
in = new ObjectInputStream(client.getInputStream());
System.out.println("(I/O) One way Road Connected...");
while(true)
{
String name=login.getText();
msgReceived.append( name+":>"+(String)in.readObject()+"\n");
System.out.println(name);
}
catch(Exception e)
{
System.out.println("Nasreen Baloch"+e.getMessage());
}
}public static void main(String[] s)
{
NetServer ns= new NetServer();
}
}
Vinod Chandana
Ranch Hand

Joined: Aug 26, 2003
Posts: 59
Hi Sandhiya,
Plz include the code in [CODE][\CODE] so that it includes the formating. Do send the client code too. When u have done that send me a message.
Regards,
Vinod.
sandhiya sindhi
Ranch Hand

Joined: Sep 25, 2003
Posts: 50
Thank you Mr vinod for such reply but i have solve this problem by my self..
now just guide me how to send more than one clients with one server and how client send message to client. I mean to say conversation between two clients.
i have tried alot but still i do not find such thing..
but yes for this purpose i have made another client and gave same pc name and port wich is in server.
but server is only making connection with one cleint.
waiting for ur reply
Thank you very much
[ November 04, 2003: Message edited by: sandhiya Laghari ]
Vinod Chandana
Ranch Hand

Joined: Aug 26, 2003
Posts: 59
Hi Sandhiya,
That is becos u r accepting the connection only once. Server must again accept a connection. I'm not sure, if two computers can communicate without one being a server.
Regards,
Vinod.
sandhiya sindhi
Ranch Hand

Joined: Sep 25, 2003
Posts: 50
hello Vinod,
thanxs again for ur help.
but mr vinod it shows that every time we need another object of client.

i clear words can i use this code as:
client1= server.accept();
client2=server.accept;
like this..
Thank you
Vinod Chandana
Ranch Hand

Joined: Aug 26, 2003
Posts: 59
Hi Sandhiya,
Exactly. Smthg like that will work. Remember if a client has to connect to a server using socket then the server should be prepared to accept the socket connection.
Regards,
Vinod.
Originally posted by sandhiya Laghari:
hello Vinod,
thanxs again for ur help.
but mr vinod it shows that every time we need another object of client.

i clear words can i use this code as:
client1= server.accept();
client2=server.accept;
like this..
Thank you
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: What is wrong in this code of Serversocket to client message
 
Similar Threads
Help!!!!!! GridBag Layout constraints
socket, gui, thread - problem
Class Container not found :(
URL query
I Got a Problem in ObjectInputStream while iam sending Vector 2nd Time