Results 1 to 4 of 4

Thread: Client not sending message to Server [Java Socket Programming]

  1. #1
    Join Date
    May 2011
    Beans
    143
    Distro
    Ubuntu 12.04 Precise Pangolin

    Client not sending message to Server [Java Socket Programming]

    Code:
    /* Server.java */
    import java.net.*;
    import java.util.*;
    import java.io.*;
    
    class Server
    {
    
            public static void main(String[] args) throws Exception
            {
                    ServerSocket server = new ServerSocket(6666);
                    Socket conn = server.accept();
                    BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
                    String a = in.readLine();
                    System.out.println(a);
            }
    
    }
    and here is the ..
    .
    Code:
    /* Client */
    import java.net.*;
    import java.io.*;
    
    class Client
    {
    
            public static void main(String[] args) throws Exception
            {
                    Socket client = new Socket();
                    client.connect(new InetSocketAddress("localhost",6666));
                    BufferedWriter out = new BufferedWriter(new OutputStreamWriter(client.getOutputStream()));               
                    out.write("Hey, this is client",0,3);
            }
    
    }
    After running i get
    Code:
    /*  SERVER  */
    meow@VikkyHacks:~/Arena/java$ javac Server.java && java Server
    null
    
    /*  CLIENT  */
    meow@VikkyHacks:~/Arena/java$ javac Client.java && java Client
    I am expecting a message after the client connects saying, "Hey, this is client"

  2. #2
    Join Date
    Sep 2006
    Location
    Germany
    Beans
    403
    Distro
    Ubuntu 11.10 Oneiric Ocelot

    Re: Client not sending message to Server [Java Socket Programming]

    Using a Terminal that you cd into the project folder, you have designated current directory, javac SocketServer.java and when that compiles you run it (with the cd still designated)

    java SocketServer

    You do have a server running waiting for one line of input. Prove it by opening another Terminal

    telnet localhost 6666
    (you get)
    Trying 127.0.0.1...
    Connected to localhost.
    Escape character is '^]'.
    Howdy from from client // this is typed in

    The Terminal window running SocketServer echos Howdy from... and exits. Because the server shut down telnet quits also.

    So, it looks like class Client.java needs PrintStream. And make sure it is ^$ cd projectFolder and run in its own Terminal.

    ====================
    // Socket programming with the streams has to be clear in your mind about which side is sending. So, to use your Server.java here is my SocClient.java

    Code:
    import java.io.IOException;
    import java.io.PrintWriter;
    import java.net.InetSocketAddress;
    import java.net.Socket;
    
    public class SocClient {
    
        public static void main(String[] args) {
    
            try {
                Socket socket = new Socket();
                socket.connect(new InetSocketAddress("localhost", 6666));
                PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
                out.println("Howdy from client");
                socket.close();
    
    
            } catch (IOException e) {
                e.printStackTrace();
            }
    
        }
    }
    Last edited by Unterseeboot_234; November 29th, 2012 at 11:06 PM. Reason: added class file
    "You don't do art because you want to, no, you do art because you have to." -- M. Buonarroti

  3. #3
    Join Date
    Jun 2007
    Location
    Maryland, US
    Beans
    6,288
    Distro
    Kubuntu

    Re: Client not sending message to Server [Java Socket Programming]

    Quote Originally Posted by vikkyhacks View Post
    I am expecting a message after the client connects saying, "Hey, this is client"
    That's a lofty expectation considering you are telling write() to only send 3 characters.

    Perhaps you should place your string literal within a String object so that you can get the string's length. After writing the string, you should flush() the buffered writer (actually, the OutputStreamWriter).

    Something like this (for the Client):
    Code:
                    String msg = "Hey, this is client";
                    out.write(msg, 0, msg.length());
                    out.flush();

  4. #4
    Join Date
    May 2011
    Beans
    143
    Distro
    Ubuntu 12.04 Precise Pangolin

    Re: Client not sending message to Server [Java Socket Programming]

    Quote Originally Posted by Unterseeboot_234 View Post
    You do have a server running waiting for one line of input.
    Code:
     PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
    Actually even I was using the same tutorial you just sent, But I don wish to use PrintWriter,
    I wanna go easy with the class like

    BufferedReader x BufferedWriter
    InputStream x OutputStream
    DataInputStream x DataOutputStream


    Quote Originally Posted by dwhitney67 View Post
    Perhaps you should place your string literal within a String object so that you can get the string's length. After writing the string, you should flush()
    Actually I did try placing using String Object but that din work and got messed up using 3 in the program, The part I din do was flush. I used your code and it works perfectly ...

Bookmarks

Posting Permissions

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