Results 1 to 10 of 10
Thread: Client server
- 01-02-2009, 09:40 PM #1
Member
- Join Date
- Jan 2009
- Posts
- 2
- Rep Power
- 0
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();
}
}
}
- 01-03-2009, 05:24 AM #2
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
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.
- 01-03-2009, 06:58 AM #3
Member
- Join Date
- Jan 2009
- Posts
- 2
- Rep Power
- 0
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
- 01-03-2009, 08:18 AM #4
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
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?
- 01-03-2009, 02:38 PM #5
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:
On the server side:Java Code:for (int i=0; i<2;i++) { if (i==0) { //send integer #1 } else { //send integer #2 }
Luck,Java Code://receive the integer //define counter if (counter is a modulo of 2) { //send sum of integers //reset counter }
CJSLChris S.
Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.
- 01-03-2009, 05:09 PM #6
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
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.
- 01-03-2009, 06:27 PM #7
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,
CJSLChris S.
Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.
- 01-04-2009, 01:37 AM #8
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
- 01-04-2009, 02:22 AM #9
Thanks...
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.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.
Thanks,
CJSLChris S.
Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.
- 01-04-2009, 04:33 PM #10
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
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
-
client-server app
By shwein in forum NetworkingReplies: 3Last Post: 10-14-2008, 05:20 PM -
Datagram Client and Server, client timer question
By saru88 in forum NetworkingReplies: 1Last Post: 10-05-2008, 03:12 PM -
how client know what kind of server
By lemur in forum NetworkingReplies: 3Last Post: 05-31-2008, 07:11 AM -
Identify Client in Socket Client Server Application
By masadjie in forum NetworkingReplies: 1Last Post: 12-20-2007, 09:18 AM -
Problems with client and server
By Albert in forum Advanced JavaReplies: 2Last Post: 07-02-2007, 06:07 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks