Commit 8d957e00785850adeba3efb0e3d34e1885dfb86f

Authored by Imanol-Mikel Barba Sabariego
1 parent 493ea8e7

git-svn-id: svn://imanolbarba.net/PAD@26 c2ee353e-ed0d-4329-bf56-03aec153487f

JChatClient/bin/client
No preview for this file type
JChatClient/src/client.cpp
... ... @@ -124,7 +124,6 @@ void* recvThread(void* args)
124 124 }
125 125 else
126 126 {
127   - *(t_arg->s) >> recv;
128 127 cout << recv << endl;
129 128 }
130 129 }
... ... @@ -173,6 +172,7 @@ int main()
173 172  
174 173 /* TODO
175 174 *
  175 + * disconnecting causes some real shit
176 176 * list nicks
177 177 * unicast message
178 178 */
179 179 \ No newline at end of file
... ...
JChatServer/bin/pad/prac2/Connection.class
No preview for this file type
JChatServer/bin/pad/prac2/MySocket.class
No preview for this file type
JChatServer/bin/pad/prac2/Server.class
No preview for this file type
JChatServer/src/pad/prac2/Connection.java
1 1 package pad.prac2;
2 2  
  3 +import java.io.IOException;
  4 +
3 5 public class Connection extends Thread
4 6 {
5 7 private MySocket socket;
... ... @@ -13,7 +15,7 @@ public class Connection extends Thread
13 15 sleep = true;
14 16 }
15 17  
16   - public void sendMessage(String message)
  18 + public void sendMessage(String message) throws IOException
17 19 {
18 20 socket.sendMsg(message);
19 21 }
... ... @@ -67,22 +69,32 @@ public class Connection extends Thread
67 69 str = socket.recvMsg();
68 70 if(str != null)
69 71 {
70   - if(str == "/disconnect")
71   - {
72   - socket.sendMsg("DISC_OK");
73   - kill = true;
74   - System.out.println(serv.getNickname(this) + " disconnected");
75   - }
76   - else if(str == "/exit")
  72 + try
77 73 {
78   - socket.sendMsg("EXIT_OK");
79   - kill = true;
80   - System.out.println(serv.getNickname(this) + " disconnected");
  74 + if(str.equals("/disconnect"))
  75 + {
  76 + socket.sendMsg("DISC_OK");
  77 + kill = true;
  78 + System.out.println(serv.getNickname(this) + " disconnected");
  79 + break;
  80 + }
  81 + else if(str.equals("/exit"))
  82 + {
  83 + socket.sendMsg("EXIT_OK");
  84 + kill = true;
  85 + System.out.println(serv.getNickname(this) + " disconnected");
  86 + break;
  87 + }
  88 + else
  89 + {
  90 + System.out.println("FROM " + serv.getNickname(this) + ": " + str);
  91 + serv.sendToChat(this,str);
  92 + }
81 93 }
82   - else
  94 + catch(IOException ioExc)
83 95 {
84   - System.out.println("FROM " + serv.getNickname(this) + ": " + str);
85   - serv.sendToChat(this,str);
  96 + System.out.println("TCP: Error writing to socket");
  97 + break;
86 98 }
87 99 }
88 100 else
... ...
JChatServer/src/pad/prac2/JChat.java
... ... @@ -31,4 +31,11 @@ public class JChat
31 31 });
32 32 serv.startServer();
33 33 }
  34 + /*
  35 + * TODO
  36 + *
  37 + * unicast message
  38 + * list nicks
  39 + * disconnect/reconnect successfully
  40 + */
34 41 }
... ...
JChatServer/src/pad/prac2/MySocket.java
... ... @@ -3,15 +3,15 @@ package pad.prac2;
3 3 import java.io.BufferedReader;
4 4 import java.io.IOException;
5 5 import java.io.InputStreamReader;
6   -import java.io.PrintWriter;
  6 +import java.io.OutputStream;
7 7 import java.net.InetSocketAddress;
8 8 import java.net.Socket;
9 9 import java.net.SocketAddress;
10 10  
11 11 public class MySocket extends Socket
12 12 {
13   - private PrintWriter output;
14 13 private BufferedReader input;
  14 + private OutputStream output;
15 15  
16 16 public MySocket()
17 17 {
... ... @@ -36,7 +36,7 @@ public class MySocket extends Socket
36 36 {
37 37 try
38 38 {
39   - output = new PrintWriter(super.getOutputStream());
  39 + output = super.getOutputStream();
40 40 input = new BufferedReader(new InputStreamReader(super.getInputStream()));
41 41 }
42 42 catch(IOException ioExc)
... ... @@ -59,11 +59,11 @@ public class MySocket extends Socket
59 59 }
60 60 }
61 61  
62   - public void sendMsg(String msg)
  62 + public void sendMsg(String msg) throws IOException
63 63 {
64 64 String length = new Integer(msg.length()).toString();
65   - this.write(length+'\0', length.length()+1);
66   - this.write(msg,msg.length());
  65 + this.write(length + '\0');
  66 + this.write(msg);
67 67 }
68 68  
69 69 public String recvMsg()
... ... @@ -80,9 +80,9 @@ public class MySocket extends Socket
80 80 return str;
81 81 }
82 82  
83   - public void write(String str, int bytes)
  83 + public void write(String str) throws IOException
84 84 {
85   - output.print(str.substring(0,bytes));
  85 + output.write(str.getBytes());
86 86 }
87 87  
88 88 public String read(int bytes)
... ...
JChatServer/src/pad/prac2/Server.java
... ... @@ -44,7 +44,7 @@ public class Server
44 44 }
45 45 }
46 46  
47   - public void sendToChat(Connection origin, String message)
  47 + public void sendToChat(Connection origin, String message) throws IOException
48 48 {
49 49 lock.lock();
50 50 String nickname = activeConnections.get(origin);
... ...