Skip to content

Instantly share code, notes, and snippets.

@badarshahzad
Created December 14, 2016 15:56
Show Gist options
  • Save badarshahzad/2407e0418e24a2cd7b33acac665bd5cc to your computer and use it in GitHub Desktop.
Save badarshahzad/2407e0418e24a2cd7b33acac665bd5cc to your computer and use it in GitHub Desktop.
This is my client, server and server-request handler can you please give me hint how I will send a packet and ACK?
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;
ObjectOutputStream oos;
Scanner input = new Scanner(System.in);
while (true) {
// receving
ois = new ObjectInputStream(client.getInputStream());
String msg = ois.readUTF();
System.out.println(msg);
// sending
oos = new ObjectOutputStream(client.getOutputStream());
String message;
System.out.print("Client:");
message = input.nextLine();
oos.writeUTF("Client:" + message);
oos.flush();
}
} catch (Exception ex) {
System.out.println(ex);
}
}
}
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 message;
public RequestHandler(Socket client){
client_acp = client;
}
@Override
public void run() {
try {
while (true) {
//sending
oos = new ObjectOutputStream(client_acp.getOutputStream());
System.out.print("Server:");
message = input.nextLine();
oos.writeUTF("Server:" + message);
oos.flush();
// receving
ois = new ObjectInputStream(client_acp.getInputStream());
String msg = ois.readUTF();
System.out.println(msg);
}
} catch (Exception e) {
System.out.println(e);
}
}
}
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);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment