From 8d957e00785850adeba3efb0e3d34e1885dfb86f Mon Sep 17 00:00:00 2001 From: Imanol-Mikel Barba Sabariego Date: Sun, 8 Dec 2013 23:51:44 +0000 Subject: [PATCH] git-svn-id: svn://imanolbarba.net/PAD@26 c2ee353e-ed0d-4329-bf56-03aec153487f --- JChatClient/bin/client | Bin 23240 -> 0 bytes JChatClient/src/client.cpp | 2 +- JChatServer/bin/pad/prac2/Connection.class | Bin 2485 -> 0 bytes JChatServer/bin/pad/prac2/MySocket.class | Bin 3240 -> 0 bytes JChatServer/bin/pad/prac2/Server.class | Bin 4827 -> 0 bytes JChatServer/src/pad/prac2/Connection.java | 40 ++++++++++++++++++++++++++-------------- JChatServer/src/pad/prac2/JChat.java | 7 +++++++ JChatServer/src/pad/prac2/MySocket.java | 16 ++++++++-------- JChatServer/src/pad/prac2/Server.java | 2 +- 9 files changed, 43 insertions(+), 24 deletions(-) diff --git a/JChatClient/bin/client b/JChatClient/bin/client index aa6f80a..683ed61 100755 Binary files a/JChatClient/bin/client and b/JChatClient/bin/client differ diff --git a/JChatClient/src/client.cpp b/JChatClient/src/client.cpp index 0d2ddea..8b688ce 100644 --- a/JChatClient/src/client.cpp +++ b/JChatClient/src/client.cpp @@ -124,7 +124,6 @@ void* recvThread(void* args) } else { - *(t_arg->s) >> recv; cout << recv << endl; } } @@ -173,6 +172,7 @@ int main() /* TODO * + * disconnecting causes some real shit * list nicks * unicast message */ \ No newline at end of file diff --git a/JChatServer/bin/pad/prac2/Connection.class b/JChatServer/bin/pad/prac2/Connection.class index 94ba3ec..0b6ee78 100644 Binary files a/JChatServer/bin/pad/prac2/Connection.class and b/JChatServer/bin/pad/prac2/Connection.class differ diff --git a/JChatServer/bin/pad/prac2/MySocket.class b/JChatServer/bin/pad/prac2/MySocket.class index 26005f9..245314e 100644 Binary files a/JChatServer/bin/pad/prac2/MySocket.class and b/JChatServer/bin/pad/prac2/MySocket.class differ diff --git a/JChatServer/bin/pad/prac2/Server.class b/JChatServer/bin/pad/prac2/Server.class index adab824..99c00c6 100644 Binary files a/JChatServer/bin/pad/prac2/Server.class and b/JChatServer/bin/pad/prac2/Server.class differ diff --git a/JChatServer/src/pad/prac2/Connection.java b/JChatServer/src/pad/prac2/Connection.java index f073466..bfabe3c 100644 --- a/JChatServer/src/pad/prac2/Connection.java +++ b/JChatServer/src/pad/prac2/Connection.java @@ -1,5 +1,7 @@ package pad.prac2; +import java.io.IOException; + public class Connection extends Thread { private MySocket socket; @@ -13,7 +15,7 @@ public class Connection extends Thread sleep = true; } - public void sendMessage(String message) + public void sendMessage(String message) throws IOException { socket.sendMsg(message); } @@ -67,22 +69,32 @@ public class Connection extends Thread str = socket.recvMsg(); if(str != null) { - if(str == "/disconnect") - { - socket.sendMsg("DISC_OK"); - kill = true; - System.out.println(serv.getNickname(this) + " disconnected"); - } - else if(str == "/exit") + try { - socket.sendMsg("EXIT_OK"); - kill = true; - System.out.println(serv.getNickname(this) + " disconnected"); + if(str.equals("/disconnect")) + { + socket.sendMsg("DISC_OK"); + kill = true; + System.out.println(serv.getNickname(this) + " disconnected"); + break; + } + else if(str.equals("/exit")) + { + socket.sendMsg("EXIT_OK"); + kill = true; + System.out.println(serv.getNickname(this) + " disconnected"); + break; + } + else + { + System.out.println("FROM " + serv.getNickname(this) + ": " + str); + serv.sendToChat(this,str); + } } - else + catch(IOException ioExc) { - System.out.println("FROM " + serv.getNickname(this) + ": " + str); - serv.sendToChat(this,str); + System.out.println("TCP: Error writing to socket"); + break; } } else diff --git a/JChatServer/src/pad/prac2/JChat.java b/JChatServer/src/pad/prac2/JChat.java index f6a57ce..45a1c1c 100644 --- a/JChatServer/src/pad/prac2/JChat.java +++ b/JChatServer/src/pad/prac2/JChat.java @@ -31,4 +31,11 @@ public class JChat }); serv.startServer(); } + /* + * TODO + * + * unicast message + * list nicks + * disconnect/reconnect successfully + */ } diff --git a/JChatServer/src/pad/prac2/MySocket.java b/JChatServer/src/pad/prac2/MySocket.java index cce5bba..2a1802a 100644 --- a/JChatServer/src/pad/prac2/MySocket.java +++ b/JChatServer/src/pad/prac2/MySocket.java @@ -3,15 +3,15 @@ package pad.prac2; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; -import java.io.PrintWriter; +import java.io.OutputStream; import java.net.InetSocketAddress; import java.net.Socket; import java.net.SocketAddress; public class MySocket extends Socket { - private PrintWriter output; private BufferedReader input; + private OutputStream output; public MySocket() { @@ -36,7 +36,7 @@ public class MySocket extends Socket { try { - output = new PrintWriter(super.getOutputStream()); + output = super.getOutputStream(); input = new BufferedReader(new InputStreamReader(super.getInputStream())); } catch(IOException ioExc) @@ -59,11 +59,11 @@ public class MySocket extends Socket } } - public void sendMsg(String msg) + public void sendMsg(String msg) throws IOException { String length = new Integer(msg.length()).toString(); - this.write(length+'\0', length.length()+1); - this.write(msg,msg.length()); + this.write(length + '\0'); + this.write(msg); } public String recvMsg() @@ -80,9 +80,9 @@ public class MySocket extends Socket return str; } - public void write(String str, int bytes) + public void write(String str) throws IOException { - output.print(str.substring(0,bytes)); + output.write(str.getBytes()); } public String read(int bytes) diff --git a/JChatServer/src/pad/prac2/Server.java b/JChatServer/src/pad/prac2/Server.java index 482e9f0..b25fa54 100644 --- a/JChatServer/src/pad/prac2/Server.java +++ b/JChatServer/src/pad/prac2/Server.java @@ -44,7 +44,7 @@ public class Server } } - public void sendToChat(Connection origin, String message) + public void sendToChat(Connection origin, String message) throws IOException { lock.lock(); String nickname = activeConnections.get(origin); -- libgit2 0.22.2