summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authordor <dor1_b@walla.com>2013-09-28 16:24:52 +0300
committerdor <dor1_b@walla.com>2013-09-28 16:24:52 +0300
commitcc2a8b879fd189aeb9caf907838d514e3633a1af (patch)
tree0b97e52b937f6c05781ce0d2959569388d7344fc
parent2c5bc88072d964fb68bead02d011766696b5ee93 (diff)
Server \client loop \ One time port
-rw-r--r--src/ChatClient.java32
-rw-r--r--src/ChatServer.java13
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();
}