Results 1 to 2 of 2
  1. #1
    Pathogenic is offline Member
    Join Date
    Apr 2011
    Posts
    1
    Rep Power
    0

    Default Why am I not receiving the message? [Sockets]

    I'm just trying to do something simple like have the computer you connect to send you a message containing the date, but I don't seem to be doing it right. I'm fairly new to java so I may be just making simple mistake but here's the code I'm trying to use as of right now;
    Java Code:
    import java.io.*;
    import java.net.*;
    
    
    public class main {
    
    	static int loop = 1;
    	static BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
    	public static void main(String[] args) throws UnknownHostException, IOException {
    		
    		connect();
    		
    }	
    		public static void connect() throws UnknownHostException, IOException{
    		System.out.print("Specify server to connect to.\n-> ");
    		String ipee = input.readLine();
    		System.out.print("Specify port to connect to.\n-> ");
    		String prt = input.readLine();
    		int port = Integer.parseInt(prt);
    		Socket client = new Socket(ipee, port);
    		InetAddress inet = client.getInetAddress();
    		BufferedReader in = new BufferedReader(new InputStreamReader(client.getInputStream()));
    		System.out.println("Connection established to "+ inet);
    		System.out.println(in.readLine());
    	}
    }
    Java Code:
    import java.io.*;
    import java.net.*;
    import java.util.*;
    
    
    public class main {
    
    	static BufferedReader out = new BufferedReader(new InputStreamReader(System.in));
    	static Date date = new Date();
    	static int loop = 1;
    	public static void main(String[] args) throws IOException {
    		
    		connect();
    
    	}
    	
    		public static void connect() throws IOException{
    			
    		while(loop == 1){
    		ServerSocket server = new ServerSocket(3030);
    		Socket client = server.accept();
    		InetAddress inet = client.getInetAddress();
    		BufferedReader in = new BufferedReader(new InputStreamReader(client.getInputStream()));
    		PrintWriter msg = new PrintWriter(client.getOutputStream(), true);
    		msg.print("Connection established on "+ date.getDate());
    		server.close();
    		client.close();
    		}
    	}
    }
    Edit: I probably should have stated the problem, which is the fact that when I connect I don't actually receive the message so may be I should be sending it another way?

  2. #2
    kjkrum's Avatar
    kjkrum is offline Senior Member
    Join Date
    Apr 2011
    Location
    Tucson, AZ
    Posts
    1,058
    Rep Power
    5

    Default

    The javadoc for that PrintWriter constructor says the autoflush option applies to the println methods. It may not be flushing because you're calling print instead of println.

Similar Threads

  1. UDP Broadcast receiving
    By Andrew121 in forum Advanced Java
    Replies: 1
    Last Post: 11-02-2010, 01:42 AM
  2. Replies: 6
    Last Post: 06-04-2010, 08:43 PM
  3. Receiving Message in J2ME
    By java girl in forum CLDC and MIDP
    Replies: 2
    Last Post: 10-27-2009, 05:49 AM
  4. File and Message transfer over sockets!
    By rameshraj in forum Networking
    Replies: 3
    Last Post: 05-14-2008, 08:13 PM
  5. Receiving UDP pockets
    By Java Tip in forum java.net
    Replies: 0
    Last Post: 04-07-2008, 08:08 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
  •