diff options
-rw-r--r-- | src/ChatClient.java | 32 | ||||
-rw-r--r-- | src/ChatServer.java | 13 |
2 files changed, 16 insertions, 29 deletions
diff --git a/src/ChatClient.java b/src/ChatClient.java index 620622a..ea0eeb9 100644 --- a/src/ChatClient.java +++ b/src/ChatClient.java @@ -9,46 +9,30 @@ public class ChatClient public static void main(String[] args) throws IOException
{
String phrase;
- System.out.println("Enter you phrase");
- phrase = reader.nextLine();
int port = ChatServer.portNum;
String host = null;
Socket Soc;
OutputStreamWriter outTemp;
- try
- {
- Soc = new Socket(host, port);
- } catch (IOException e)
- {
- System.err.println("Error connecting to host " + host + ", port " + port + ": " + e);
- return;
- }
- try
- {
- outTemp = new OutputStreamWriter(Soc.getOutputStream());
- } catch (IOException e)
- {
- System.err.println("getOutputStream failed (should not have happened): " + e);
- Soc.close();
- return;
- }
+
+ Soc = new Socket(host, port);
+ outTemp = new OutputStreamWriter(Soc.getOutputStream());
BufferedWriter output = new BufferedWriter(outTemp);
InputStreamReader inputTemp = new InputStreamReader(Soc.getInputStream());
BufferedReader input = new BufferedReader(inputTemp);
while (true)
{
-
-
+ System.out.print("Write here:> ");
+ phrase = reader.nextLine();
output.write(phrase, 0, phrase.length());
output.newLine();
output.flush();
phrase = input.readLine(); // FIXME: more then 1 line
phrase = "[" + phrase + "]";
System.out.println(phrase);
-
+
}
-
- Soc.close();
+
+ //Soc.close();
}
}
diff --git a/src/ChatServer.java b/src/ChatServer.java index ee4c83b..30a0811 100644 --- a/src/ChatServer.java +++ b/src/ChatServer.java @@ -6,16 +6,18 @@ public class ChatServer {
public static Scanner reader = new Scanner(System.in);
- public static int portNum = 40000;
+ public static int portNum = 40001;
+
public static void main(String[] args) throws IOException
{
String phrase;
ServerSocket Service = new ServerSocket(portNum);
+ Socket Soc = null;
try
{
+ Soc = Service.accept();
while (true)
{
- Socket Soc = Service.accept();
InputStreamReader inputTemp = new InputStreamReader(Soc.getInputStream());
BufferedReader input = new BufferedReader(inputTemp);
phrase = input.readLine(); // FIXME: more then 1 line
@@ -27,12 +29,13 @@ public class ChatServer output.write(phrase, 0, phrase.length());
output.newLine();
output.flush();
- Soc.close();
+
System.out.println("Done");
}
- }
- catch (IOException e)
+ } catch (IOException e)
{
+ if (Soc != null)
+ Soc.close();
System.out.println(e);
Service.close();
}
|