Skip to content

Instantly share code, notes, and snippets.

@drelagreen
Created October 18, 2018 20:23
Show Gist options
  • Save drelagreen/6cc1803b1463a9a6c445681746c7327a to your computer and use it in GitHub Desktop.
Save drelagreen/6cc1803b1463a9a6c445681746c7327a to your computer and use it in GitHub Desktop.
NetworkChatServer 1.0+
package Serv;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
public class Server1 {
static volatile Map<String, Integer> clients = new HashMap<>();
static volatile ArrayList<T> online = new ArrayList<>();
static volatile ServerSocket serverSocket;
public static void main(String[] args) throws IOException {
char[] c = {'4','6','5','4'};
char[] c3 = {'5','2','2','5'};
System.out.println(Arrays.hashCode(c));
char[] c1 = {'1','2','3','4'};
char[] c2 = {'1','1','3','4'};
clients.put("Dimon", Arrays.hashCode(c));
clients.put("Pukich", Arrays.hashCode(c3));
clients.put("Kakich", Arrays.hashCode(c1));
clients.put("HehMdaHmm", Arrays.hashCode(c2));
kek();
}
static void newConnect(String nick){
sendMessage("! > "+nick , "/вошёл в чат!/");
sendMessage3("!theOnline");
}
static void offConnect(T t, String nick){
System.out.println("htoto otletel (2)");
for (int i = 0; i < online.size(); i++) {
if (online.get(i).nick.equals(nick)) {
online.remove(i);
t.hmm = false;
t.socket = null;
t.interrupt();
sendMessage3("!theOnline");
sendMessage("! > "+nick, "/отлетел!/");
}
}
}
static void sendMessage3(String m){
if (m.equals("!theOnline")){
StringBuilder x= new StringBuilder();
x.append("ONLINE:\n");
for (T t : online) {
x.append(t.nick);
x.append("\n");
}
for (T t : online) {
t.sMessage("3"+x.toString());
}
}
}
static void sendMessage(String nick, String m){
System.out.println(m);
if (online.size()!=0)
for (int i = 0; i < online.size(); i++) {
if (!online.get(i).isInterrupted())
if (!online.get(i).nick.equals(nick))
online.get(i).sMessage("1"+nick+" > "+m);
}
}
static void cmd(String com){
}
static void kek() throws IOException {
serverSocket = new ServerSocket(7788);
while (true) {
try {
System.out.println("&");
T t = new T(serverSocket.accept());
t.start();
System.out.println("+1");
} catch (IOException e) {
e.printStackTrace();
System.out.println("/kek/ error");
break;
}
}
}
}
class T extends Thread{
Boolean hmm = false;
Socket socket;
String nick;
DataInputStream dataInputStream;
DataOutputStream dataOutputStream;
T(Socket socket){
this.socket=socket;
}
@Override
public void run() {
try {
dataInputStream = new DataInputStream(socket.getInputStream());
dataOutputStream = new DataOutputStream(socket.getOutputStream());
String welcome = dataInputStream.readUTF();
String w1[] = welcome.split(":");
String[] x = w1[1].split("");
if (Server1.clients.get(w1[0])==Integer.parseInt(w1[1])){
System.out.println(w1[0]+"n"+w1[1]);
dataOutputStream.writeBoolean(true);
for (int i = 0; i < Server1.online.size(); i++) {
if (Server1.online.get(i).nick.equals(w1[1])){
dataOutputStream.writeBoolean(false);
throw new IOException();
}
}
nick = w1[0];
dataOutputStream.writeUTF("N"+nick);
Server1.online.add(this);
inMessageDaemon();
hmm=true;
Server1.newConnect(nick);
} else {
System.out.println("wrongPass! de pas is - " + Server1.clients.get(w1[0]) + " but " + w1[1]);
dataOutputStream.writeBoolean(false);
}
} catch (IOException e) {
System.out.println("htoto otletel");
e.printStackTrace();
if (hmm)
Server1.offConnect(this, nick);
}
}
public void sMessage(String m){
try {
dataOutputStream.writeUTF(m);
System.out.println(m);
} catch (IOException e) {
if(hmm) {
System.out.println("htoto otletel");
e.printStackTrace();
Server1.offConnect(this, nick);
}
}
}
public void inMessageDaemon(){
new Thread(()->{
while (true){
try {
String m = dataInputStream.readUTF();
Server1.sendMessage(nick, m);
} catch (IOException e) {
System.out.println("htoto otletel");
e.printStackTrace();
Server1.offConnect(this, nick);
break;
}
}
}).start();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment