hi,
i want create a server and client in java.
after connecting,client should send 4 byte integer to server.
how can i do this?
please help me.
thanks,....
Printable View
hi,
i want create a server and client in java.
after connecting,client should send 4 byte integer to server.
how can i do this?
please help me.
thanks,....
hi,
thanks for reply.
yes i have heard about those.i connected to the server.now i want to send 4 byte iteger to server.how can i do it?i am a student.
pls help me
There are several options: you could send those numbers as text (using a PrintWriter wrapped around the OutputStream and a BufferedReader around the InputStream). You can also use DataInputStreams and DataOutputStreams and send the data in binary. Read the API documentation of those classes.
kind regards,
Jos
go take a look here
thanks everyone.
i use this for the server.
input= new ObjectInputStream(server.getInputStream());
output=new ObjectOutputStream(server.getOutputStream());
output.flush();
System.out.println("received "+input.readInt());
and this for the client.
output=new ObjectOutputStream(client.getOutputStream());
output.flush();
output.writeInt(7);
input= new ObjectInputStream(client.getInputStream());
client connects with the server.but sending integers does not working and client terminates the connection.how can i solve that.
pls help me.
Better use DataInputStream and DataOutputStream, they're much simpler. I guess your scenario suffers from the fact that an ObjectOutputStream tries to write a header block first (when it just has been opened) which isn't read by the other tier yet.
kind regards,
Jos
i changed that into datainputstram and dataOuptputStream.
now when i run client i got this error message.
java.net.ConnectException: Connection refused: connect
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.PlainSocketImpl.doConnect(PlainSocketImpl .java:333)
closing connection
at java.net.PlainSocketImpl.connectToAddress(PlainSoc ketImpl.java:195)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.j ava:182)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.j ava:366)
at java.net.Socket.connect(Socket.java:507)
at java.net.Socket.connect(Socket.java:457)
at java.net.Socket.<init>(Socket.java:365)
at java.net.Socket.<init>(Socket.java:207)
at Client.connectToServer(Client.java:67)
at Client.runClient(Client.java:35)
at ClientTest.main(ClientTest.java:18)
Exception in thread "main" java.lang.NullPointerException
at Client.closeConnection(Client.java:121)
at Client.runClient(Client.java:57)
at ClientTest.main(ClientTest.java:18)
could you pls tel me why?and how can i solve that issue?
hi everyone,
now above issue is ok.
but server does not connect with client.How can i make sure that accept method is working or not?
Whats the error message you are receiving? Can you please show us the code that is generating the issue, or at least part of the code?
Thanks
hi,
this for the server to accept the client .
System.out.print("waiting for connection");
connection=server.accept();
System.out.print("connection received from"connection.getInetAddress().getHostName());
when I run the sreverTest i only get following OP
waiting for connection
thats all.why?
After you ran the server, did you run your client?
yes.
I use this method for the client.
private void connectToServer() throws IOException
{
System.out.println("Attempting connection");
client=new Socket(InetAddress.getByName(chatServer),13345);
System.out.println("connected to"+client.getInetAddress().getHostName());
}
then for streams i use this in client class.
private void getStreams() throws IOException
{
output=new DataOutputStream(client.getOutputStream());
output.flush();
output.writeInt(7);
input= new DataInputStream(client.getInputStream());
System.out.println("hai server");
}
to test the client I use,
Client application;
application = new Client("127.0.0.1");
application.runClient();
i get the output as below.
Attempting connection
connected tolocalhost
hai server
closing connection
where is the problem?
thanks....
More importantly -- did you go through the client-server tutorials, step by step?
could you pls tell me a good reference for that?I went through some tutorials.
my server waits connetion for long time.how can I fix that?
pls help me.im very cinfusing.i am a beginner.
thanks
pls help me
I saw you started another thread for exactly the same problem. Below you'll find a bare bones server (it can only serve one client and doesn't do anything useful). The client simply pumps whatever it reads from System.in to the server. Here's the code for the server:
And here's the code for the client:Code:import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;
class Server {
public static void main(String[] args) throws Exception {
ServerSocket ss= new ServerSocket(1234);
Socket s= ss.accept();
InputStream is= s.getInputStream();
int i;
while ((i= is.read()) != -1)
System.out.println(i);
is.close();
s.close();
ss.close();
}
}
Try to use this code as a base for your code. My code only uses primitive input and output streams. You can wrap them in more sophisticated streams if needed.Code:import java.io.OutputStream;
import java.net.InetAddress;
import java.net.Socket;
public class Client {
public static void main(String[] args) throws Exception {
Socket s= new Socket(InetAddress.getByName(null), 1234);
OutputStream os= s.getOutputStream();
int i;
while ((i= System.in.read()) != -1)
os.write(i);
os.close();
s.close();
}
}
kind regards,
Jos