Results 1 to 5 of 5
  1. #1
    Vortexnl is offline Member
    Join Date
    Feb 2011
    Posts
    8
    Rep Power
    0

    Default 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:

    Java Code:
    	public void sendMessage()
    	{
    		scanner = new Scanner(System.in);
    		String msg = scanner.nextLine();
    		out.println(msg);
    		
    		sendMessage();
    	}
    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...

    Dear god I need help

    Moderator Edit: Code tags added
    Last edited by Fubarable; 02-05-2011 at 09:38 PM. Reason: Moderator Edit: Code tags added

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    You understand that this method is recursive?

    Java Code:
    	public void sendMessage()
    	{
    		scanner = new Scanner(System.in);
    		String msg = scanner.nextLine();
    		out.println(msg);
    		
    		sendMessage();
    	}
    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.

    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.

  3. #3
    Vortexnl is offline Member
    Join Date
    Feb 2011
    Posts
    8
    Rep Power
    0

    Default

    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 10:01 PM.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by Vortexnl View Post
    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.

    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!

  5. #5
    Vortexnl is offline Member
    Join Date
    Feb 2011
    Posts
    8
    Rep Power
    0

    Default

    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

  1. Replies: 10
    Last Post: 06-06-2010, 07:49 PM
  2. can't send messages correctly [chat]
    By michail in forum New To Java
    Replies: 1
    Last Post: 05-11-2010, 11:24 AM
  3. Replies: 7
    Last Post: 03-28-2009, 07:20 PM
  4. How to send messages / events ??
    By Gatts79 in forum AWT / Swing
    Replies: 3
    Last Post: 11-13-2008, 06:51 PM
  5. how can server send messages every 5 min?
    By dim_ath in forum Networking
    Replies: 7
    Last Post: 01-10-2008, 04:59 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •