Page 1 of 2 12 LastLast
Results 1 to 20 of 26
  1. #1
    krishforever is offline Member
    Join Date
    Jan 2011
    Posts
    19
    Rep Power
    0

    Smile creating a server and client in java

    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,....

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,515
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by krishforever View Post
    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,....
    Have you read about Sockets and ServerSockets? Those objects can give you InputStreams and OutputStreams (you can build Readers and Writers on top of them if you want) and you're in business.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    krishforever is offline Member
    Join Date
    Jan 2011
    Posts
    19
    Rep Power
    0

    Default

    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.

  4. #4
    krishforever is offline Member
    Join Date
    Jan 2011
    Posts
    19
    Rep Power
    0

    Default

    pls help me

  5. #5
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,515
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by krishforever View Post
    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
    cenosillicaphobia: the fear for an empty beer glass

  6. #6
    JavaHater is offline Senior Member
    Join Date
    Dec 2010
    Posts
    165
    Rep Power
    4

    Default

    Quote Originally Posted by krishforever View Post
    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,....
    go take a look here

  7. #7
    krishforever is offline Member
    Join Date
    Jan 2011
    Posts
    19
    Rep Power
    0

    Smile

    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.

  8. #8
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,515
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by krishforever View Post
    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
    cenosillicaphobia: the fear for an empty beer glass

  9. #9
    krishforever is offline Member
    Join Date
    Jan 2011
    Posts
    19
    Rep Power
    0

    Default

    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?

  10. #10
    krishforever is offline Member
    Join Date
    Jan 2011
    Posts
    19
    Rep Power
    0

    Smile

    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?

  11. #11
    user0 is offline Senior Member
    Join Date
    Dec 2010
    Posts
    100
    Rep Power
    0

    Default

    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--

  12. #12
    krishforever is offline Member
    Join Date
    Jan 2011
    Posts
    19
    Rep Power
    0

    Smile

    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?

  13. #13
    user0 is offline Senior Member
    Join Date
    Dec 2010
    Posts
    100
    Rep Power
    0

    Default

    After you ran the server, did you run your client?
    --user0--

  14. #14
    krishforever is offline Member
    Join Date
    Jan 2011
    Posts
    19
    Rep Power
    0

    Default

    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....

  15. #15
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

  16. #16
    krishforever is offline Member
    Join Date
    Jan 2011
    Posts
    19
    Rep Power
    0

    Default

    could you pls tell me a good reference for that?I went through some tutorials.

  17. #17
    krishforever is offline Member
    Join Date
    Jan 2011
    Posts
    19
    Rep Power
    0

    Default

    my server waits connetion for long time.how can I fix that?

  18. #18
    krishforever is offline Member
    Join Date
    Jan 2011
    Posts
    19
    Rep Power
    0

    Default

    pls help me.im very cinfusing.i am a beginner.
    thanks

  19. #19
    krishforever is offline Member
    Join Date
    Jan 2011
    Posts
    19
    Rep Power
    0

    Default

    pls help me

  20. #20
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,515
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by krishforever View Post
    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:

    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();
    	}
    }
    And here's the code for the client:

    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();
    	}
    }
    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.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Page 1 of 2 12 LastLast

Similar Threads

  1. Java Server/C Client
    By FallenBlade in forum Networking
    Replies: 13
    Last Post: 03-10-2011, 11:22 PM
  2. Replies: 2
    Last Post: 09-24-2010, 11:31 AM
  3. java server and c client ?????????
    By biebo in forum Networking
    Replies: 7
    Last Post: 07-24-2010, 04:35 AM
  4. Java Message Server/Client help
    By sari in forum Networking
    Replies: 3
    Last Post: 03-20-2010, 11:17 PM
  5. Creating an IRC client in Java
    By VeasMKII in forum New To Java
    Replies: 0
    Last Post: 06-17-2009, 09:19 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •