Results 1 to 5 of 5
- 02-05-2011, 06:41 PM #1
Member
- Join Date
- Feb 2011
- Posts
- 8
- Rep Power
- 0
Two-way chat, how can I be able to send messages?
So I'm currently working on a 2-way chat system, because I am new to java this is kinda hard for me, I have a function that allows the user to enter a message, but this function never gets reached because its busy with the server part...
this is the code of the server:
Java Code:import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.net.ServerSocket; import java.net.Socket; import java.util.Scanner; public class Server { static ServerSocket server = null; static Socket client = null; static BufferedReader in = null; static PrintWriter out = null; Scanner scanner = null; public static void main(String[] args) { try { server = new ServerSocket(9999); System.out.println("Server initiated at port 9999...\n"); } catch (IOException e) { System.out.println("Could not listen on port 9999"); System.exit(-1); } try { client = server.accept(); } catch (IOException e) { System.out.println("Accept failed: 9999"); System.exit(-1); } try { in = new BufferedReader(new InputStreamReader(client.getInputStream())); out = new PrintWriter(client.getOutputStream(), true); } catch (IOException e) { System.out.println("Read failed"); System.exit(-1); } while(true) { try { String line = in.readLine(); out.println(line); } catch (IOException e) { System.out.println("Read failed"); System.exit(-1); } } } }
And I want to be able to use this method:
How the hell do I make this work? When I put the method on top, it won't run my other code because its always busy with this method...Java Code:public void sendMessage() { scanner = new Scanner(System.in); String msg = scanner.nextLine(); out.println(msg); sendMessage(); }
Dear god I need help
Moderator Edit: Code tags addedLast edited by Fubarable; 02-05-2011 at 08:38 PM. Reason: Moderator Edit: Code tags added
-
You understand that this method is recursive?
Meaning that it is calling itself over and over again, without end, and I have a feeling that this is not what you want to do.Java Code:public void sendMessage() { scanner = new Scanner(System.in); String msg = scanner.nextLine(); out.println(msg); sendMessage(); }
I think that the best way that you can help us help you is if you could clearly tell us just what it is you are trying to do, both in overall non-java programming terms and more specifically, because I'm not sure how these two snips of code are supposed to relate. What's your overall goal? Do you have a class and code for your client? Are you using proper threading techniques? Have you gone through the server/client section of the java tutorials?
Much luck!
Also, I added code tags to your code to help make it more readable. to see how to do this yourself, please look at the first link in my signature.
- 02-05-2011, 08:58 PM #3
Member
- Join Date
- Feb 2011
- Posts
- 8
- Rep Power
- 0
Hey Fubarable, I appriciate your comment!
What I am trying to do is make a very basic 2-way chat system, so the server can send and receive messages from the client.
The 'sendMessage' method was doing the input part, but I didn't know anything about Java and the execution of code (All programming knowledge I have is from AS3)
Since I have only been busy with Java for about three days, I don't know a lot, but I did some research and found out what 'Threads' are...
I guess I'll have to take a look at making a seperate class that implements the runnable class, so I can do multiple things at the same time.
Edit: Thanks for adding those code blocks, I couldn't find them in the editor so I didn't use them! (all I saw was HTML block and PHP block)Last edited by Vortexnl; 02-05-2011 at 09:01 PM.
-
Whoa! If you can quickly create a working chat application after such a brief exposure to Java, I will know that you are truly a genius of high order. If however you are a mere mortal like me, you'll need to study the basics of the language a bit first before attempting to digest an intermediate-level project such as this one.
Edit: Thanks for adding those code blocks, I couldn't find them in the editor so I didn't use them! (all I saw was HTML block and PHP block)
No problem. I wish you much luck!
- 02-05-2011, 09:21 PM #5
Member
- Join Date
- Feb 2011
- Posts
- 8
- Rep Power
- 0
Don't worry, I already got A LOT of the basics ;)
I have exposed myself to an overload of java tutorials in the past few days, but in the end all I want is a socket server that can connect to flash (I don't want to use it unless I know how it works ;) )
Thanks for the help Fubarable! Java is an awesome language, and I can't wait to see how far I will be in 2 weeks...
Similar Threads
-
simple send email APP, but when press send button appeared:
By lse123 in forum Advanced JavaReplies: 10Last Post: 06-06-2010, 06:49 PM -
can't send messages correctly [chat]
By michail in forum New To JavaReplies: 1Last Post: 05-11-2010, 10:24 AM -
Help sending messages using TCP with small chat application
By Ernie- in forum NetworkingReplies: 7Last Post: 03-28-2009, 06:20 PM -
How to send messages / events ??
By Gatts79 in forum AWT / SwingReplies: 3Last Post: 11-13-2008, 05:51 PM -
how can server send messages every 5 min?
By dim_ath in forum NetworkingReplies: 7Last Post: 01-10-2008, 03:59 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks