0
   

Problem in UDP client/server application

 
 
Reply Mon 7 Jun, 2010 05:34 pm
Hey everyone,

i'm interested in creating a client/server application in which server stores the names and phone numbers of 10 persons and when client sends a name, the server looks up in the array. If the name send by the client is present in the array, it sends the corresponding phone no to the client.....

The code goes something like this:

SERVER CODE:
import java.net.*;
import java.io.*;
import java.util.*;

class NamePhone {
public static void main(String [] args) {
int clientport = 111, serverport = 5000;
String array[] = new String[10];
Scanner a = new Scanner(System.in);
byte buffer[] = new byte[100];
try {
DatagramSocket ds = new DatagramSocket(5000);
System.out.println("Enter the Name and phone no of 10 persons");
for(int i=0;i<3;i++)
{
String str = a.nextLine();
array = str;
}
//while(true) {
System.out.println("Server waiting for client to send Name");
DatagramPacket dp = new DatagramPacket(buffer,buffer.length);
ds.receive(dp);
String s = new String(dp.getData(),0,dp.getData().length);
for(int i=0;i<3;i++)
{
String s1 = array;
System.out.println("s1 is "+s1);
System.out.println("s is " +s);
System.out.println(s1.indexOf(s));
String s2 = "pda123";
String s3= "pda";
System.out.println("pda123.indexOf(pda) is"+ s2.indexOf(s3));
if(s1.indexOf(s)!=-1)
{
System.out.println("Im going in for"+i);
//System.out.println("Yes, the name is found and the request from "+ InetAddress.getByName(dp.getAddress()).getHostName()+"is accepted");
System.out.println("The phone number of "+s+" is : " + s1.substring(s.length()));
}
}
//}
}
catch(Exception e) {}
}
}




CLIENT CODE:
import java.io.*;
import java.net.*;
import java.util.*;

class ClientSendingName {
public static void main(String [] args) {
try {
int clientport = 111, serverport = 5000;
Scanner a = new Scanner(System.in);
byte buffer[] = new byte[100];
System.out.println("Enter the name whose phone number needs to be find out");
String s = a.nextLine();
buffer = s.getBytes();
DatagramSocket ds = new DatagramSocket(111);
while(true) {
DatagramPacket dp = new DatagramPacket(buffer,buffer.length,InetAddress.getByName("pda"),serverport);
ds.send(dp);
}
}
catch(Exception e) {}
}
}

However the problem that im facing is that the function indexOF() returns -1 even if the name typed by the client is present in the array.
for example:
if s1 = the string which takes data of array present at evry index = pda123
and s = name send by client = pda
then also
s1.indexOf(s) returns -1

However when i manually took two strings of same name pda123 and pda and applied the same function, then it returns 0 as expected.

I can't understand how is it even possible....
It is blowing up my mind. Please help urgently.....
  • Topic Stats
  • Top Replies
  • Link to this Topic
Type: Question • Score: 0 • Views: 920 • Replies: 0
No top replies

 
 

Related Topics

 
  1. Forums
  2. » Problem in UDP client/server application
Copyright © 2024 MadLab, LLC :: Terms of Service :: Privacy Policy :: Page generated in 0.03 seconds on 04/16/2024 at 04:33:34