Results 1 to 10 of 10

Thread: Client server

  1. #1
    yash is offline Member
    Join Date
    Jan 2009
    Posts
    2
    Rep Power
    0

    Default Client server

    I have a Client server programs and it have to modified to send tow Num from Client to server and server doing the summation of these tow num and replay it the client in UDP
    the following are the tow program and if any one can help me

    Client program
    import java.net.*;
    import java.io.*;

    public class UDPClient
    {
    public static void main(String args[])
    {

    String args0="Hello from client";
    String args1="localhost";

    DatagramSocket aSocket=null;
    try
    {
    aSocket=new DatagramSocket();

    byte[]m=args0.getBytes();

    InetAddress aHost=InetAddress.getByName(args1);

    int serverPort=3789;

    DatagramPacket request=
    new DatagramPacket(m,args0.length(),aHost,serverPort);

    aSocket.send(request);

    byte[]buffer=new byte[1000];

    DatagramPacket reply=
    new DatagramPacket(buffer,buffer.length);

    aSocket.receive(reply);

    System.out.println("Client> message received is: "+new String(reply.getData()));
    }
    catch(SocketException e)
    {
    System.out.println("Socket:"+e.getMessage());
    }
    catch(IOException e)
    {
    System.out.println("IO:"+e.getMessage());

    }
    finally
    {
    if(aSocket!=null)
    aSocket.close();
    }
    }
    }



    server side
    package udpclientos;
    import java.net.*;
    import java.io.*;
    public class UDPServer
    {
    public static void main(String args[])
    {
    DatagramSocket aSocket =null;
    try
    {
    aSocket= new DatagramSocket(3789);
    byte[]buffer=new byte[1000];
    while(true)
    {
    DatagramPacket request =new DatagramPacket(buffer,buffer.length);
    aSocket.receive(request);
    System.out.println("Server> message received is: "+new String(request.getData()));
    DatagramPacket reply=new DatagramPacket(request.getData(),request.getLength (),request.getAddress(),request.getPort());
    aSocket.send(reply);
    System.out.println("Server> message sent is: " +new String(reply.getData()));
    }
    }
    catch(SocketException e)
    {
    System.out.println("Socket:"+e.getMessage());
    }
    catch(IOException e)
    {
    System.out.println("IO:"+e.getMessage());
    }
    finally
    {
    if(aSocket!=null)
    aSocket.close();
    }
    }
    }

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    Seems this is not your own code. If so you must know how send a message from the client and how server listen it. Best thing is go for a tutorial on Java networking and study the basis first of all.

  3. #3
    yash is offline Member
    Join Date
    Jan 2009
    Posts
    2
    Rep Power
    0

    Default modification needed

    yes this not my code

    I tried to define tow integers in the client side to send it to server side
    in the server side ,it must compute the summation between then and replay with the sum to the client

    i tried but I have no results

  4. #4
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    Ok, did you run the server and client, and did you check what happen there?

    Can you see that client send a message and server receive it?

  5. #5
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    7

    Default The apps work

    I tried them both last night and they work. The message travels from client code to server code and back again to client. There are probably many ways to do this. One I can think of is:

    On client side: use a for loop to send the two integers:
    Java Code:
    for (int i=0; i<2;i++)
    {
    	if (i==0)
    	{
    	 //send integer #1
    	}
    	else
    	{
    	 //send integer #2
    	}
    On the server side:
    Java Code:
    //receive the integer 
    //define counter
    if (counter is a modulo of 2)
    {
     //send sum of integers
     //reset counter 
    }
    Luck,
    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  6. #6
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    For me the best way is work on the client side.

    Anyway, our thread starter must have an idea what happen on the code at the time he has. Otherwise how can he doing modifications on it. I think still he doesn't know from where(which line of the code) send the message to the server.

  7. #7
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    7

    Default working...

    I finally got it working.... a quick resume of the things that I had to do:

    Client side:
    Beside the for loop in my previous post...
    • trim the response from the server code with the trim() method


    Server side:
    • also trim the data received from the client
    • convert to integer to do the sum
    • convert the sum back to a string
    • convert string to byte array (so it can be used in the DatagramPacket constructor)
    • send datagram back to client

    @OP: I strongly suggest that you try to understand the flow of both the client and server code. You don't have to be a datagram expert (I'm certainly not one).

    Now for the forum... I had to do the Integer-String-byte array circus because I couldn't find a way to directly convert from Integer to byte array. I tried a code that I found on the web, but it wasn't converting correctly (lost a lot of time with that). So... any hints on how to convert directly from Integer to byte array?

    Thanks & luck,
    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  8. #8
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    Quote Originally Posted by CJSLMAN View Post
    Now for the forum... I had to do the Integer-String-byte array circus because I couldn't find a way to directly convert from Integer to byte array. I tried a code that I found on the web, but it wasn't converting correctly (lost a lot of time with that). So... any hints on how to convert directly from Integer to byte array?
    I think that's the way you have to do with Integer byte conversion. If you have int primitive types then there are lots of ways. Sorry lol, I don't know any simple way here.

  9. #9
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    7

    Default Thanks...

    I think that's the way you have to do with Integer byte conversion. If you have int primitive types then there are lots of ways. Sorry lol, I don't know any simple way here.
    Eranga... thanks a lot. I'll goof around with the code to see if I can find a prettier solution. If I find one, I'll post it.

    Thanks,
    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  10. #10
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    NP lol, I just let you know what I have learned. Anyway, if you found any simple solution please send it here. Yes it's really helpful, and I'll try to work it out too later.

Similar Threads

  1. client-server app
    By shwein in forum Networking
    Replies: 3
    Last Post: 10-14-2008, 05:20 PM
  2. Replies: 1
    Last Post: 10-05-2008, 03:12 PM
  3. how client know what kind of server
    By lemur in forum Networking
    Replies: 3
    Last Post: 05-31-2008, 07:11 AM
  4. Identify Client in Socket Client Server Application
    By masadjie in forum Networking
    Replies: 1
    Last Post: 12-20-2007, 09:18 AM
  5. Problems with client and server
    By Albert in forum Advanced Java
    Replies: 2
    Last Post: 07-02-2007, 06:07 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
  •