Ali Payandeh
Ali Payandeh

Reputation: 63

defining both client and server in main

I am new to Java Socket programming. I try to use NIO socket. I follow some code on client and server side. I can use each of them separately. but when I use both in main, Just the first on will start. I want to know first if it it is logical to doing this and second how I can manage this problem:

public static void main(String[] args) {
 NioSocketServer server = new NioSocketServer();
 NioSocketClient client = new NioSocketClient();

Upvotes: 0

Views: 61

Answers (1)

Ashkan Kazemi
Ashkan Kazemi

Reputation: 1097

You need to create a ServerSocket (or NioSocketServer as you need) :

ServerSocket server = new ServerSocket(8080);
Socket client = server.accept();
// get data from client
// do your processing here and then 
// make your reponse into a String msg
Socket secondMachine = new Socket(secondMachineAddress, secondMachinePort);
secondMachine.getOutputStream().write(msg.getBytes()); // something like this!
secondMachine.getOutputStream().flush();

of course the code is just a schema and it is only to give you an idea how it is done, it won't work (or compile!).

Upvotes: 1

Related Questions