Results 1 to 17 of 17
- 05-01-2009, 04:18 PM #1
Member
- Join Date
- Apr 2009
- Posts
- 50
- Rep Power
- 0
[SOLVED] TCP chat test availability server
Hallo,
I did these programs:
Java Code:import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStream; import java.io.OutputStreamWriter; import java.net.InetSocketAddress; import java.net.ServerSocket; import java.net.Socket; import java.net.UnknownHostException; import java.util.logging.Level; import java.util.logging.Logger; /** * * @author Lolek */ public class Main { /** * @param args the command line arguments */ public static void main(String[] args) { String addressIP; if(args.length==0) { addressIP="127.0.0.1"; } else { addressIP=args[0]; } ReciveT recive = new ReciveT(); SendT send = new SendT(addressIP); recive.server.start(); send.client.start(); } } class ReciveT implements Runnable { Thread server; public ReciveT() { server = new Thread(this,"Server Thread"); } public void run() { try { ServerSocket serverSocket = new ServerSocket(5000); Socket clientSocket = null; clientSocket = serverSocket.accept(); while(true) { BufferedReader read = new BufferedReader(new InputStreamReader(clientSocket.getInputStream())); String line = null; while((line = read.readLine())!=null) { System.out.println(line); System.out.flush(); } } } catch (IOException ex) { Logger.getLogger(ReciveT.class.getName()).log(Level.SEVERE, null, ex); } } } class SendT implements Runnable { private String addressIP; Thread client; public SendT(String ip) { this.addressIP=ip; client = new Thread(this,"Client Thread"); } public void run() { try { Thread.sleep(10000); InetSocketAddress server = new InetSocketAddress(this.addressIP, 4000); Socket socket = new Socket(this.addressIP, 4000); BufferedReader readKey = new BufferedReader(new InputStreamReader(System.in)); OutputStream output = socket.getOutputStream(); BufferedWriter write = new BufferedWriter(new OutputStreamWriter(output)); String line; while ((line = readKey.readLine()) != null) { write.write(line); write.newLine(); write.flush(); System.out.println("Text:" + line); } } catch (InterruptedException ex) { Logger.getLogger(OdeslatVlakno.class.getName()).log(Level.SEVERE, null, ex); } catch (UnknownHostException ex) { Logger.getLogger(OdeslatVlakno.class.getName()).log(Level.SEVERE, null, ex); } catch (IOException ex) { Logger.getLogger(OdeslatVlakno.class.getName()).log(Level.SEVERE, null, ex); } } }
Java Code:import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStream; import java.io.OutputStreamWriter; import java.net.InetSocketAddress; import java.net.ServerSocket; import java.net.Socket; import java.net.UnknownHostException; import java.util.logging.Level; import java.util.logging.Logger; /** * * @author Lolek */ public class Main { /** * @param args the command line arguments */ public static void main(String[] args) { String addressIP; if(args.length==0) { addressIP="127.0.0.1"; } else { addressIP=args[0]; } ReciveT recive = new ReciveT(); SendT send = new SendT(addressIP); recive.server.start(); send.client.start(); } } class ReciveT implements Runnable { Thread server; public ReciveT() { server = new Thread(this,"Server Thread"); } public void run() { try { ServerSocket serverSocket = new ServerSocket(4000); Socket clientSocket = null; clientSocket = serverSocket.accept(); while(true) { BufferedReader read = new BufferedReader(new InputStreamReader(clientSocket.getInputStream())); String line = null; while((line = read.readLine())!=null) { System.out.println(line); System.out.flush(); } } } catch (IOException ex) { Logger.getLogger(ReciveT.class.getName()).log(Level.SEVERE, null, ex); } } } class SendT implements Runnable { private String addressIP; Thread client; public SendT(String ip) { this.addressIP=ip; client = new Thread(this,"Client Thread"); } public void run() { try { Thread.sleep(10000); InetSocketAddress server = new InetSocketAddress(this.addressIP, 5000); Socket socket = new Socket(this.addressIP, 5000); BufferedReader readKey = new BufferedReader(new InputStreamReader(System.in)); OutputStream output = socket.getOutputStream(); BufferedWriter write = new BufferedWriter(new OutputStreamWriter(output)); String line; while ((line = readKey.readLine()) != null) { write.write(line); write.newLine(); write.flush(); System.out.println("Text:" + line); } } catch (InterruptedException ex) { Logger.getLogger(OdeslatVlakno.class.getName()).log(Level.SEVERE, null, ex); } catch (UnknownHostException ex) { Logger.getLogger(OdeslatVlakno.class.getName()).log(Level.SEVERE, null, ex); } catch (IOException ex) { Logger.getLogger(OdeslatVlakno.class.getName()).log(Level.SEVERE, null, ex); } } }
- 05-01-2009, 05:05 PM #2
What does "test availability server" mean?
Don't forget to mark threads as [SOLVED] and give reps to helpful posts.
How To Ask Questions The Smart Way
- 05-01-2009, 06:07 PM #3
Member
- Join Date
- Apr 2009
- Posts
- 50
- Rep Power
- 0
Trying if the server is reachable.
- 05-01-2009, 06:14 PM #4
You attempt to connect and see if it works, just as you have done. No need for the sleep().
Don't forget to mark threads as [SOLVED] and give reps to helpful posts.
How To Ask Questions The Smart Way
- 05-01-2009, 06:35 PM #5
Member
- Join Date
- Apr 2009
- Posts
- 50
- Rep Power
- 0
And How can i do this thing? I am beginer. Becouse now this error: java.net.ConnectException: Connection refused: connect
- 05-01-2009, 06:59 PM #6
Catch the error and deal with it. If there's nothing you can do then just print the error message.
Further points:
You don't need to flush System.out
You do need to close the socket once you're done with it
You need to move serverSocket.accept() into the while loop
You need to have both the server and the client use the same portDon't forget to mark threads as [SOLVED] and give reps to helpful posts.
How To Ask Questions The Smart Way
- 05-01-2009, 07:16 PM #7
Member
- Join Date
- Apr 2009
- Posts
- 50
- Rep Power
- 0
I think you donītīunderstand my code or I donīt understand you. My program is client and server too and connect on same program because ServerT need diferent port number and ClientT need diferent port number.
- 05-01-2009, 07:35 PM #8
If you're trying to get SendT to talk to RecieveT they have to connect to the same port. A client and server can only communicate if they are using the same port. It's also much easier to test this program if you make the server and client into separate applications, then you can start the client once you know the server is set up.
Don't forget to mark threads as [SOLVED] and give reps to helpful posts.
How To Ask Questions The Smart Way
- 05-01-2009, 07:48 PM #9
Member
- Join Date
- Apr 2009
- Posts
- 50
- Rep Power
- 0
No I donīt want this. I know ports and this stuff. This program copy to another PC and connect together. I donīt need central server becouse every program is server also client. OK? Sory for my English.
- 05-02-2009, 10:22 AM #10
Then why have you hard-coded the port in 2 complete code duplicates? Use command line args.
But why not just use one connection? Sockets have both input and output streams. Having a different port number for every client is going to be unmanageable.
The solution to your original problem is to either have a command that tries to connect, or just retry the connection attempt until successful.Don't forget to mark threads as [SOLVED] and give reps to helpful posts.
How To Ask Questions The Smart Way
- 05-02-2009, 10:50 AM #11
Member
- Join Date
- Apr 2009
- Posts
- 50
- Rep Power
- 0
I think that, Every client donīt need different port number it is just on localhost. And if i use just one connection, One program was server and second client or no? I do this for small LAN about 10 computers no big deal.
And could you write sample code how can i do this stuff? Thanks
- 05-02-2009, 11:03 AM #12
Ignore that one connection comment - wasn't thinking properly.
Here's your current set-up: Client 1 listens on port x and sends to client 2 on port y. Client 2 listens on port y and sends to client 1 on port x. What is client 3 supposed to do?
What you want is for every client to listen and connect to the same port number (ServerSocket and Socket bound to same port). Then any client can talk to any other. You won't be able to run two copies on the same machine, but then why would you want to.Don't forget to mark threads as [SOLVED] and give reps to helpful posts.
How To Ask Questions The Smart Way
- 05-02-2009, 11:18 AM #13
Member
- Join Date
- Apr 2009
- Posts
- 50
- Rep Power
- 0
Yes i know. Communication can be only between 2 computers. But if I want that, Communication was between more computers, I must to creat for each client new Thread. Maybe i will do this later. But now i want do just very sipmle chat. I am beginner, I program just 2 months.
- 05-02-2009, 11:22 AM #14
I have already provided solutions that will get your test working.
Don't forget to mark threads as [SOLVED] and give reps to helpful posts.
How To Ask Questions The Smart Way
- 05-02-2009, 11:41 AM #15
Member
- Join Date
- Apr 2009
- Posts
- 50
- Rep Power
- 0
And could you write sample code? I would very grateful.
- 05-02-2009, 12:31 PM #16
Sample code for what?
Java Code:try { while (true) { try { socket = new Socket(this.addressIP, PORT); break; } catch (IOException e) { System.err.println(e.getMessage); Thread.sleep(10000); } } } catch (InterruptedException e) { System.out.println("Terminating on INT"); return; }
Don't forget to mark threads as [SOLVED] and give reps to helpful posts.
How To Ask Questions The Smart Way
- 05-02-2009, 01:55 PM #17
Member
- Join Date
- Apr 2009
- Posts
- 50
- Rep Power
- 0
Similar Threads
-
Multithread Chat server/client
By gwaldarick in forum Advanced JavaReplies: 3Last Post: 09-19-2009, 01:22 AM -
[SOLVED] UDP chat client server
By Koren3 in forum NetworkingReplies: 2Last Post: 04-25-2009, 02:51 AM -
Server.java Errors trying to build a chat
By agfre44_9873 in forum Java AppletsReplies: 2Last Post: 04-25-2009, 02:49 AM -
simple chat server
By sari in forum New To JavaReplies: 0Last Post: 02-06-2009, 03:30 AM -
JDK availability in Testing server
By curesh in forum Suggestions & FeedbackReplies: 6Last Post: 01-26-2009, 04:33 AM
Bookmarks