2
\$\begingroup\$

The code is working perfectly. I'm little bit sure this is a packet what I am sending from client to server in streams. Is this packet? Am I sending in a right way?

Another thing is that I am sending ACK from server to client. Is this right way to send ACK and NACK? Moreover, I am parsing on client side the ACK and checking again. What would you recommend? Can you share your experience?

Client:

import java.io.*;
import java.net.*;
import java.util.Scanner;

public class Client {

public static void main(String args[]) {

    try {
        Socket client = new Socket("localhost", 2983);
        ObjectInputStream ois = null;
        ObjectOutputStream oos = null;
        Scanner input = new Scanner(System.in);

        int [] num = new int[]{1,2,3,4,5};
        for (int a=0;a<num.length;){
            // sending
            System.out.print("Client send packet: "+num[a]+"\t");
            oos = new ObjectOutputStream(client.getOutputStream());
            oos.writeUTF(String.valueOf(num[a]));
            oos.flush();

            // receving
            ois = new ObjectInputStream(client.getInputStream());
            String msg = ois.readUTF();
            System.out.println(msg);
            String[] flag = msg.split(":");
            if(flag[0].equals("ACK")){
                a++;
            }else{
                a--;
            }
        }
        System.out.println("By to server");

        ois.close();
        oos.close();
        client.close();
    } catch (Exception ex) {
        System.out.println(ex);
    }
}

}

Server:

import java.io.*;
import java.net.*;
import java.util.Scanner;

public class Server {

    public static void main(String args[]) {
        try {
            ServerSocket server_socket = new ServerSocket(2983);
            System.out.println("Waiting for client request");

            while (true) {
                Socket client = server_socket.accept();
                System.out.println("Accepted connection request");
                RequestHandler obj = new RequestHandler(client);
                obj.start();
            }
        } catch (Exception e) {
            System.out.println(e);
        }

        // Scanner input = new Scanner(System.in);
    }
}

RequestHandler:

import java.io.*;
import java.net.*;
import java.util.Scanner;

public class RequestHandler extends Thread{

    Scanner input = new Scanner(System.in);
    Socket client_acp;
    ObjectInputStream ois;
    ObjectOutputStream oos;
    String packet_num;
    public RequestHandler(Socket client){
        client_acp = client;
    }

    @Override
    public void run() {

        try {
            while (true) {
                // receving
                ois = new ObjectInputStream(client_acp.getInputStream());
                String msg = ois.readUTF();
                System.out.println(msg);
                packet_num=msg;
                if(!(packet_num.equals(null))){
                //sending
                System.out.print("Server:");
                oos = new ObjectOutputStream(client_acp.getOutputStream());
                oos.writeUTF("ACK: From Server Packet = "+ packet_num );
                oos.flush();
                }else{
                    System.out.print("Server:");
                    oos = new ObjectOutputStream(client_acp.getOutputStream());
                    oos.writeUTF("NACK: From Server Packet = "+ packet_num);
                    oos.flush();
                }
            }
        } catch (Exception e) {
            System.out.println(e);
        }
    }
}
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.