Skip to main content

java client server program

1 reply [Last post]
krish4ever
Offline
Joined: 2011-01-08
Points: 0

hi all,
I need a help in this.
What I have to do is creating the client and server in java.
then client make connection with server.
then client send 4 byte integer and 1 byte character.
how can i do this?
pls helpme
im a beginner.

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
vincenzod
Offline
Joined: 2010-12-30
Points: 0

The server and the client exchange serialized java objects, if you create a serialized class wich contains an integer istance and a character instance, you will be able to exchange
this object of this instanced class from client to server or viceversa. For instance, you have to create the class ToSend:

I didn't tested it .... but try ;)

import java.io.Serializable;

/**

* @author 

*/

public class ToSend implements Serializable {


        private int theIntegerNumber ;

    
        private char characterToSend ;

      public ToSend(char character,int number){



                this.characterToSend = character;

                this.theIntegerNumber = number;

               

       

        }
        public char getChar() {

                        return this.characterToSend;

               

        }
        public int getNumber() {

                return this.theIntegerNumber;

        }

}

The server

import java.io.*;
import java.net.*;
public class Provider{
ServerSocket providerSocket;
Socket connection = null;
ObjectOutputStream out;
ObjectInputStream in;
String message;
Provider(){}
void run()
{
try{
//1. creating a server socket
providerSocket = new ServerSocket(2004, 10);
//2. Wait for connection
System.out.println("Waiting for connection");
connection = providerSocket.accept();
System.out.println("Connection received from " + connection.getInetAddress().getHostName());
//3. get Input and Output streams
out = new ObjectOutputStream(connection.getOutputStream());
out.flush();
in = new ObjectInputStream(connection.getInputStream());
sendMessage("Connection successful");
//4. The two parts communicate via the input and output streams
do{
try{
message = (ToSend)in.readObject();
System.out.println("client>" + message.getNumber+"\n"+message.getC);
if (message.equals("bye"))
sendMessage("bye");
}
catch(ClassNotFoundException classnot){
System.err.println("Data received in unknown format");
}
}while(!message.equals("bye"));
}
catch(IOException ioException){
ioException.printStackTrace();
}
finally{
//4: Closing connection
try{
in.close();
out.close();
providerSocket.close();
}
catch(IOException ioException){
ioException.printStackTrace();
}
}
}
void sendMessage(String msg)
{
try{
out.writeObject(msg);
out.flush();
System.out.println("server>" + msg);
}
catch(IOException ioException){
ioException.printStackTrace();
}
}
public static void main(String args[])
{
Provider server = new Provider();
while(true){
server.run();
}
}
}
 

The client

  import java.io.*;
import java.net.*;
public class Requester{
Socket requestSocket;
ObjectOutputStream out;
ObjectInputStream in;
String message;
Requester(){}
void run()
{
try{
//1. creating a socket to connect to the server
requestSocket = new Socket("localhost", 2004);
System.out.println("Connected to localhost in port 2004");
//2. get Input and Output streams
out = new ObjectOutputStream(requestSocket.getOutputStream());
out.flush();
in = new ObjectInputStream(requestSocket.getInputStream());
//3: Communicating with the server
do{
try{
message = (String)in.readObject();
System.out.println("server>" + message);
sendMessage(new ToSend(5,'a'));
message = "bye";
sendMessage(new ToSend(5,'b'));
}
catch(ClassNotFoundException classNot){
System.err.println("data received in unknown format");
}
}while(!message.equals("bye"));
}
catch(UnknownHostException unknownHost){
System.err.println("You are trying to connect to an unknown host!");
}
catch(IOException ioException){
ioException.printStackTrace();
}
finally{
//4: Closing connection
try{
in.close();
out.close();
requestSocket.close();
}
catch(IOException ioException){
ioException.printStackTrace();
}
}
}
void sendMessage(ToSend msg)
{
try{
out.writeObject(msg);
out.flush();
}
catch(IOException ioException){
ioException.printStackTrace();
}
}
public static void main(String args[])
{
Requester client = new Requester();
client.run();
}
}
 

Sorry for the indentation....