Created
December 14, 2016 15:56
-
-
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?
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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