Results 1 to 20 of 26
- 01-09-2011, 11:20 AM #1
Member
- Join Date
- Jan 2011
- Posts
- 19
- Rep Power
- 0
- 01-09-2011, 11:23 AM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,606
- Blog Entries
- 7
- Rep Power
- 17
When people rob a bank they get a penalty; when banks rob people they get a bonus.
- 01-09-2011, 12:01 PM #3
Member
- Join Date
- Jan 2011
- Posts
- 19
- Rep Power
- 0
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.
- 01-09-2011, 12:28 PM #4
Member
- Join Date
- Jan 2011
- Posts
- 19
- Rep Power
- 0
pls help me
- 01-09-2011, 12:30 PM #5
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,606
- Blog Entries
- 7
- Rep Power
- 17
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,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 01-09-2011, 12:40 PM #6
Senior Member
- Join Date
- Dec 2010
- Posts
- 165
- Rep Power
- 3
go take a look here
- 01-09-2011, 07:04 PM #7
Member
- Join Date
- Jan 2011
- Posts
- 19
- Rep Power
- 0
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.
- 01-09-2011, 07:13 PM #8
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,606
- Blog Entries
- 7
- Rep Power
- 17
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,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 01-10-2011, 02:44 AM #9
Member
- Join Date
- Jan 2011
- Posts
- 19
- Rep Power
- 0
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?
- 01-10-2011, 03:29 AM #10
Member
- Join Date
- Jan 2011
- Posts
- 19
- Rep Power
- 0
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?
- 01-10-2011, 03:56 AM #11
Senior Member
- Join Date
- Dec 2010
- Posts
- 100
- Rep Power
- 0
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--user0--
- 01-10-2011, 04:17 AM #12
Member
- Join Date
- Jan 2011
- Posts
- 19
- Rep Power
- 0
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?
- 01-10-2011, 04:39 AM #13
Senior Member
- Join Date
- Dec 2010
- Posts
- 100
- Rep Power
- 0
After you ran the server, did you run your client?
--user0--
- 01-10-2011, 05:07 AM #14
Member
- Join Date
- Jan 2011
- Posts
- 19
- Rep Power
- 0
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?
- 01-10-2011, 05:21 AM #16
Member
- Join Date
- Jan 2011
- Posts
- 19
- Rep Power
- 0
could you pls tell me a good reference for that?I went through some tutorials.
- 01-10-2011, 06:17 AM #17
Member
- Join Date
- Jan 2011
- Posts
- 19
- Rep Power
- 0
my server waits connetion for long time.how can I fix that?
- 01-10-2011, 06:19 AM #18
Member
- Join Date
- Jan 2011
- Posts
- 19
- Rep Power
- 0
pls help me.im very cinfusing.i am a beginner.
thanks
- 01-10-2011, 06:58 AM #19
Member
- Join Date
- Jan 2011
- Posts
- 19
- Rep Power
- 0
pls help me
- 01-10-2011, 07:52 AM #20
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,606
- Blog Entries
- 7
- Rep Power
- 17
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:Java 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.Java 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,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
Similar Threads
-
Java Server/C Client
By FallenBlade in forum NetworkingReplies: 13Last Post: 03-10-2011, 11:22 PM -
server-client; client sends a username to the server.
By lkcz in forum New To JavaReplies: 2Last Post: 09-24-2010, 11:31 AM -
java server and c client ?????????
By biebo in forum NetworkingReplies: 7Last Post: 07-24-2010, 04:35 AM -
Java Message Server/Client help
By sari in forum NetworkingReplies: 3Last Post: 03-20-2010, 11:17 PM -
Creating an IRC client in Java
By VeasMKII in forum New To JavaReplies: 0Last Post: 06-17-2009, 09:19 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks