Brandon Lengefeld
Brandon Lengefeld

Reputation: 1

Java Server And Client Socket Need Fix

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.

Upvotes: 0

Views: 784

Answers (4)

Brandon Lengefeld
Brandon Lengefeld

Reputation: 1

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();
}

Upvotes: 0

Germann Arlington
Germann Arlington

Reputation: 3353

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.

Upvotes: 1

Reimeus
Reimeus

Reputation: 159784

Make sure you flush the output from your server:

stream.flush();

Upvotes: 0

Kumar Vivek Mitra
Kumar Vivek Mitra

Reputation: 33534

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");

Upvotes: 0

Related Questions