Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

Java 

Simple Java Chat Server / Client

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! 
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
(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).
A beautiful share, excellent work OneHalf :)
[Image: giphy-2-1.gif]
Nice, will play around a bit with this! Thank you :)
(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!
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.
(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


Possibly Related Threads...
Thread / Author Replies Views Last Post
[C] by gurlhackerclubAug 31, 2020 09:34 PM 0 185 Aug 31, 2020 09:34 PM
Last Post: gurlhackerclub
[C] by aeiroMar 14, 2018 02:48 AM 7 453 Mar 14, 2018 02:48 AM
Last Post: inyourmemory
[Java] by +ErvinasJul 29, 2017 02:16 PM 7 275 Jul 29, 2017 02:16 PM
Last Post: matt