PDA

View Full Version : Client/Server in Java


jimbob666
01-25-2010, 11:51 AM
Hoping someone can help me with this. I have two classes, Client and Server (below) which currently when run send and receive packets and repeat messages sent between them. I need to change this so that the server contains a word, for example 'cat' as a String, and the client is set up so that when the user types 'cat' it searches the server for that word, prints it if it is there, or says if it is not on the server.

import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;

public class Server {

public static void main(String[] args) throws Exception {

DatagramSocket serverSocket = new DatagramSocket(1234); // Create new UDP Socket bound to port 1234. You can type any number you like here.
byte[] receiveData = new byte[1024]; // Specify size of UDP chunk to receive.
byte[] sendData = new byte[1024]; // Specify size of UDP chunk to send.

while (true) // Infinite loop.
{
DatagramPacket receivePacket = new DatagramPacket(receiveData,receiveData.length);
serverSocket.receive(receivePacket);
String receiveString = new String(receivePacket.getData());

InetAddress IPAddress = receivePacket.getAddress(); // Get the IP Address of the received packet
int portNumber = receivePacket.getPort(); // Get the port of the received packet

System.out.println("From Client: " + IPAddress.toString() + "\t Port: " + portNumber + "\t Message: "+ receiveString);

sendData = receiveString.getBytes();
DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, IPAddress, portNumber);
serverSocket.send(sendPacket); // Send a new packet back to the same location as the received packet (an echo)
}
}
}

import java.io.*;
import java.net.*;

public class Client {
public static void main(String[] args) throws IOException {

System.out.println("Type something here, then press Enter: ");
BufferedReader userInput = new BufferedReader(new InputStreamReader(System.in));

DatagramSocket clientSocket = new DatagramSocket();
InetAddress IPAddress = InetAddress.getByName("localhost"); // You can replace localhost with the server IP, e.g: 127.0.0.1

byte[] receiveData = new byte[1024]; // Specify size of UDP chunk to receive.
byte[] sendData = new byte[1024]; // Specify size of UDP chunk to send.

String inputString = userInput.readLine();
sendData = inputString.getBytes();

DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, IPAddress, 1234); // Replace number with server port number
clientSocket.send(sendPacket);
DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);

clientSocket.receive(receivePacket);

System.out.println("From Server: " + new String(receivePacket.getData()));
clientSocket.close();
}
}

Old Pedant
01-25-2010, 06:54 PM
Is this homework?

If it is, then surely you have some specification from the instructor on what kind of search you are supposed to do on the server.

If it's not, then why not start by creating a simple demo to help yourself get started: Maybe just have a static array of words on the server and, when you receive the packet, break the received string into words (split the input string on spaces, perhaps) and test each word to see if it is in the static array. When found, send yourself (your client) a simple message such as "found: cat dog zucchini".