0

Hello, I am new to java socket programming and I was just looking to see if somebody could give me some help.

I will post the code for the client and server then i will explain my problem...

            reader = new BufferedReader(new InputStreamReader(socket.getInputStream));

        while(running)
        {
            String line = reader.readLine();

            if(line != null)
            {
                System.out.println(line);
                stream = new PrintStream(socket.getOutputStream());
                stream.println("return: " + line);
            }

        }

    }catch(IOException e)
    {
        System.out.println("Socket in use or not available: " + port);
    }
}

public static void main()
{
    run();
}


//Client

public static String ip;
public static int port;

public static Socket socket;
public static PrintStream stream;
public static BufferedReader reader;

public static void main(String args[])
{

    try
    {
        socket = new socket(ip, port);
        stream = new PrintStream(socket.getOutputStream());
        stream.println("test0");
        reader = new BufferedReader(new  InputStreamReader(socket.getInputStream));
        String line = reader.readLine();

        if(line != null)
        {
            System.out.println(line);
        }   

        stream.println("test1");
        line = reader.readLine();

        if(line != null)
        {
            System.out.println(line);   
        }   
    }catch(IOException e)
    {
            System.out.println("could not connect to server!");
    }
}

So my problem is even if I get rid of the loop and try to make it send the string twice it won't send it. It will only do it once unless I close and make a new socket on the client side. So if anybody could give me an explanation to what I am doing wrong that would be great, and thank you so much.

1
  • If the result of readLine() is null you must exit the loop and close the socket. Otherwise you will just fry the CPU forever. Commented Sep 25, 2012 at 1:46

4 Answers 4

1

Why are you openning your outstream inside your loop? stream = new PrintStream(socket.getOutputStream());

Take this statement outside the loop and write to your stream inside your loop.

Sign up to request clarification or add additional context in comments.

Comments

0

Please keep it simple,

Try using InputStream, InputStreamReader, BufferedReader, OutputStream, PrintWriter.

Client Side:

Socket s = new Socket();
s.connect(new InetSocketAddress("Server_IP",Port_no),TimeOut); 
// Let Timeout be 5000

Server Side:

ServerSocket ss = new ServerSocket(Port_no);
Socket incoming = ss.accept();

For Reading from the Socket:

InputStream is = s.getInputStream();
InputStreamReader isr = new InputStreamReader(is);

BufferedReader br = new BufferedReader(isr);
boolean isDone = false;

String s = new String();

while(!isDone && ((s=br.readLine())!=null)){

     System.out.println(s);   // Printing on Console

 }

For Writing to the Socket:

OutputStream os = s.getOuptStream();
PrintWriter pw = new PrintWriter(os)

pw.println("Hello");

2 Comments

thanks for replying i will toy around with this code and see how i can get it to work. Thanks!
I have tried every change suggested so far but no luck. It is probably my fault for not being clear enough so sorry about that. But what my issue is, is when i send something like "test0" it will make it to the server and resend no problem but then if i where to send "test1" no luck going or coming from to/from the server. is it not supposed to keep a steady stream or am i doing something wrong?
0

Make sure you flush the output from your server:

stream.flush();

Comments

0

Thanks a lot to everybody who answered but i figures out what it was all along. i read through some of the Oracle socket stuff and figured out that the server was supposed to be the first to send a message than the client receive and send and receive... so on and so forth so i will post my new code here in hopes somebody else trying to figure out the same thing can find it with ease

    //Client    
public static String ip;
public static int port;

public static Socket socket;
public static PrintWriter print;
public static BufferedReader reader;

public Client(String ip, int port)
{
    this.ip = ip;
    this.port = port;

            //initiate all of objects
    try 
    {
        socket = new Socket();
        socket.connect(new InetSocketAddress(ip, port), 5000);
        print = new PrintWriter(socket.getOutputStream());
        reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            //start connection with server
        String line = reader.readLine();
        System.out.println(line);
    } catch (IOException e) 
    {
        e.printStackTrace();
    }
}

    //quick method to send message
public void sendMessage(String text)
{
    print.println(text);
    print.flush();
    try 
    {
        String line = reader.readLine();
        System.out.println(line);
    } catch (IOException e) 
    {
        e.printStackTrace();
    }
}
}

public static void main(String args[])
{
    client.sendMessage("test");
    client.sendMessage("test2");
    client.sendMessage("test3")
}

    //Server


public static int port = 9884;
public static boolean running = true;

public static ServerSocket serverSocket;
public static Socket socket;
public static PrintWriter writer;
public static BufferedReader reader;

public static void run()
{
    try 
    {
        serverSocket = new ServerSocket(port);
        socket = serverSocket.accept();
        writer = new PrintWriter(socket.getOutputStream(), true);
        writer.println("connection");

        while(running)
        {
            reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            String line = reader.readLine();

            if(line != null)
            {
                System.out.println(line);
                writer.println(line);
                writer.flush();
            }
        }
    } catch (IOException e) 
    {
        e.printStackTrace();
    }
}

public static void main(String args[])
{
    run();
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.