Hack Forums
[Java] Simple Java Chat Server / Client - Printable Version

+- Hack Forums (https://hackforums.net)
+-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444)
+--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151)
+---- Forum: Software Development (https://hackforums.net/forumdisplay.php?fid=118)
+---- Thread: [Java] Simple Java Chat Server / Client (/showthread.php?tid=5780561)



Simple Java Chat Server / Client - OneHalf - Jan 6, 2018

Hey everyone - I'm sure some of you have seen I'm working on an MMORPG - and I was working on a chat client for it and I just (semi) finished it. It's a functional chat client with no GUI yet but I decided to post it separate because a few days ago I saw someone posting asking about making a chat client in Java so here it is! If you have any questions please post below - this isn't a tutorial so I'm not going to dissect the code.


Server.java


Code
package dev.omega.OmegaOnline.connect;

import java.net.ServerSocket;
import java.io.*;
import java.net.*;

public class Server {

static ServerSocket serverSocket;
static Socket socket;
static DataOutputStream out;
static Users[]user = new Users[10];
static DataInputStream in;

public static void main(String[] args) throws Exception {
System.out.println("Starting server....");
serverSocket = new ServerSocket(7777);
System.out.print("Server started....");
while(true) {
socket = serverSocket.accept();
for(int i = 0; i < 10;i++) {

System.out.println("Connection from: " + socket.getInetAddress());
out = new DataOutputStream(socket.getOutputStream());
in = new DataInputStream(socket.getInputStream());
if(user[i] == null) {
user[i] = new Users(out,in,user);
Thread thread = new Thread(user[i]);
thread.start();
break;
}
}
}
}
}
class Users implements Runnable{

DataOutputStream out;
DataInputStream in;
Users[]user = new Users[10];
String username;

public Users(DataOutputStream out, DataInputStream in,Users[]user) {
this.out = out;
this.in = in;
this.user = user;
}

public void run() {
try {
username = in.readUTF();
} catch (IOException e1) {
e1.printStackTrace();
}
while(true) {
try {
String message = in.readUTF();
for (int i =0 ; i < 10; i++) {

if(user[i] != null) {
user[i].out.writeUTF(username + ":" + message);
}
}
}catch(IOException e) {
this.out = null;
this.in = null;

}

}

}

}





Client.java

Code
package dev.omega.OmegaOnline.connect;

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

public class Client {

static Socket socket;
static DataInputStream in;
static DataOutputStream out;


public static void main(String[] args) throws Exception {
System.out.println("Connecting....");
socket = new Socket("localhost",7777);
System.out.println("Connection successful!");
in = new DataInputStream(socket.getInputStream());
out = new DataOutputStream(socket.getOutputStream());
Input input = new Input(in);
Thread thread = new Thread(input);
thread.start();
Scanner sc = new Scanner(System.in);
System.out.println("Enter your username then press enter!");
String username = sc.nextLine();
out.writeUTF(username);
while(true) { 
String sendMessage = sc.nextLine();
out.writeUTF(sendMessage);
}

}

}

class Input implements Runnable{

DataInputStream in;

public Input(DataInputStream in) {
this.in = in;
}

public void run() {
while(true) {
String message;
try {
message = in.readUTF();
System.out.println(message);
} catch (IOException e) {
e.printStackTrace();
}

}

}

}






How to use -> In your IDE create "Server.java" and "Client.java" Copy paste code -> Run "Server" -> Run "Client" twice! (It's multi threaded and connected) So you can switch between client 1 & client 2 to see how the chat works! 



RE: Simple Java Chat Server / Client - matt - Jan 7, 2018

May I recommend you use java.nio's SocketChannel & ServerSocketChannel unless you want blocking io but I don't think that's ever the case


RE: Simple Java Chat Server / Client - Aussie - Jan 7, 2018

(Jan 6, 2018 02:19 PM)OneHalf Wrote:  
Hey everyone - I'm sure some of you have seen I'm working on an MMORPG - and I was working on a chat client for it and I just (semi) finished it. It's a functional chat client with no GUI yet but I decided to post it separate because a few days ago I saw someone posting asking about making a chat client in Java so here it is! If you have any questions please post below - this isn't a tutorial so I'm not going to dissect the code.


Server.java


Code
package dev.omega.OmegaOnline.connect;

import java.net.ServerSocket;
import java.io.*;
import java.net.*;

public class Server {

static ServerSocket serverSocket;
static Socket socket;
static DataOutputStream out;
static Users[]user = new Users[10];
static DataInputStream in;

public static void main(String[] args) throws Exception {
System.out.println("Starting server....");
serverSocket = new ServerSocket(7777);
System.out.print("Server started....");
while(true) {
socket = serverSocket.accept();
for(int i = 0; i < 10;i++) {

System.out.println("Connection from: " + socket.getInetAddress());
out = new DataOutputStream(socket.getOutputStream());
in = new DataInputStream(socket.getInputStream());
if(user[i] == null) {
user[i] = new Users(out,in,user);
Thread thread = new Thread(user[i]);
thread.start();
break;
}
}
}
}
}
class Users implements Runnable{

DataOutputStream out;
DataInputStream in;
Users[]user = new Users[10];
String username;

public Users(DataOutputStream out, DataInputStream in,Users[]user) {
this.out = out;
this.in = in;
this.user = user;
}

public void run() {
try {
username = in.readUTF();
} catch (IOException e1) {
e1.printStackTrace();
}
while(true) {
try {
String message = in.readUTF();
for (int i =0 ; i < 10; i++) {

if(user[i] != null) {
user[i].out.writeUTF(username + ":" + message);
}
}
}catch(IOException e) {
this.out = null;
this.in = null;

}

}

}

}





Client.java

Code
package dev.omega.OmegaOnline.connect;

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

public class Client {

static Socket socket;
static DataInputStream in;
static DataOutputStream out;


public static void main(String[] args) throws Exception {
System.out.println("Connecting....");
socket = new Socket("localhost",7777);
System.out.println("Connection successful!");
in = new DataInputStream(socket.getInputStream());
out = new DataOutputStream(socket.getOutputStream());
Input input = new Input(in);
Thread thread = new Thread(input);
thread.start();
Scanner sc = new Scanner(System.in);
System.out.println("Enter your username then press enter!");
String username = sc.nextLine();
out.writeUTF(username);
while(true) { 
String sendMessage = sc.nextLine();
out.writeUTF(sendMessage);
}

}

}

class Input implements Runnable{

DataInputStream in;

public Input(DataInputStream in) {
this.in = in;
}

public void run() {
while(true) {
String message;
try {
message = in.readUTF();
System.out.println(message);
} catch (IOException e) {
e.printStackTrace();
}

}

}

}






How to use -> In your IDE create "Server.java" and "Client.java" Copy paste code -> Run "Server" -> Run "Client" twice! (It's multi threaded and connected) So you can switch between client 1 & client 2 to see how the chat works! 

Very nice. This is more worth it than using a short cut method like a library like kryonet (which a lot of people use).


RE: Simple Java Chat Server / Client - weld - Jan 7, 2018

A beautiful share, excellent work OneHalf :)


RE: Simple Java Chat Server / Client - Kronk1337 - Jan 7, 2018

Nice, will play around a bit with this! Thank you :)


RE: Simple Java Chat Server / Client - OneHalf - Jan 7, 2018

(Jan 7, 2018 01:47 PM)weld Wrote:  A beautiful share, excellent work OneHalf :)

Thank you! Appreciate the feed back!


(Jan 7, 2018 02:49 PM)Kronk1337 Wrote:  Nice, will play around a bit with this! Thank you :)

Hey thanks , and enjoy! If you have any questions let me know!


RE: Simple Java Chat Server / Client - RoundLettuce - Oct 3, 2024

Okay. This is really cool!
I have a question, but I have no idea if you're active. I hope you reply! :)
I'm a newb in Java, please can you tell me what this line does?

package dev.omega.OmegaOnline.connect;

What is it connecting to? Is it an external source? Java library?. I'm sooo confused! Sorry!
Hope you can reply please, OneHalf.


RE: Simple Java Chat Server / Client - APIBoy - Apr 30, 2026

(Oct 3, 2024 10:39 AM)RoundLettuce Wrote:  Okay. This is really cool!
I have a question, but I have no idea if you're active. I hope you reply! :)
I'm a newb in Java, please can you tell me what this line does?

package dev.omega.OmegaOnline.connect;

What is it connecting to? Is it an external source? Java library?. I'm sooo confused! Sorry!
Hope you can reply please, OneHalf.

That's just the namespace bro. Helps organise code, avoid naming conflicts