Skip to content

Instantly share code, notes, and snippets.

@hemmrich
Last active December 29, 2015 06:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hemmrich/7631101 to your computer and use it in GitHub Desktop.
Save hemmrich/7631101 to your computer and use it in GitHub Desktop.
Networking with multiple clients
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
import java.net.UnknownHostException;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextField;
public class Client extends JFrame {
static DataOutputStream out;
static DataInputStream in;
static int portNum = 45000;
public static void main(String args[]) throws UnknownHostException,
IOException {
Client cGUI = new Client();
cGUI.setVisible(true);
Socket cSocket = new Socket("127.0.0.1", portNum);
out = new DataOutputStream(cSocket.getOutputStream());
in = new DataInputStream(cSocket.getInputStream());
while(true) {
@SuppressWarnings("deprecation")
String fromServer = in.readLine();
if(fromServer != null)
System.out.println("FROM SERVER: " + fromServer);
}
}
Client() {
final JTextField tfInput;
JButton bSend;
tfInput = new JTextField(40);
bSend = new JButton("Send to Server");
bSend.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
String input = tfInput.getText().toString();
try {
System.out.println("Sending: " + input);
input += '\n';
out.writeChars(input);
} catch (IOException e) {
System.out.println("Error sending String");
}
}
});
setLayout(new FlowLayout());
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
add(tfInput);
add(bSend);
pack();
}
}
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
public class MiniServer extends Thread {
private Socket socket = null;
static int clientNum;
static DataInputStream iStream;
static DataOutputStream oStream;
public MiniServer(Socket socket) {
super("MiniServer");
this.socket = socket;
}
public void run() {
System.out.println("Client " + clientNum + " Connected");
clientNum++;
try {
iStream = new DataInputStream(socket.getInputStream());
oStream = new DataOutputStream(socket.getOutputStream());
} catch (IOException e) {
System.out.println("ERROR: getting InputStream or OutputStream");
}
String data;
while(true) {
//if data has been received from client
if((data = getInput(iStream)) != null) {
System.out.println(data);
data += '\n';
for(int i = 0; i < Server.allClients.length; i++) {
try {
Server.allClients[i].oStream.writeChars(data);
} catch (IOException e) {
System.out.println("ERROR: writing to allClients");
}
}
}
else {
System.out.println("No viable data received from client");
System.out.println("Client may have disconnected. Exiting program.");
System.exit(-1);
}
}
}
// helpful methods
@SuppressWarnings("deprecation")
public String getInput(DataInputStream in) {
String receivedData = "";
try {
receivedData = in.readLine();
} catch (IOException e) {
System.out.println("ERROR: getting data");
System.exit(-1);
}
return receivedData;
}
}
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
public class Server {
static MiniServer[] allClients; //contains all connected clients
public static void main(String[] args) throws IOException {
ServerSocket serverSocket = null;
try {
serverSocket = new ServerSocket(45000);
} catch (IOException e) {
System.err.println("Could not listen on port: 45000");
System.exit(-1);
}
int i = 0;
allClients = new MiniServer[4];
while(i < 4){
Socket clientSocket = serverSocket.accept();
MiniServer mini = new MiniServer(clientSocket);
allClients[i] = mini;
mini.start();
System.out.println("Server " + i + " started");
i++;
}
//serverSocket.close();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment