I have created two socket file,But it is not working in proper way.
please let meknow where the porblem is.

TCPClient.java

import java.io.*;
import java.net.*;

class TCPClient
{
    public static void main(String ar[]) throws Exception
    {
    String fromserver;
    String toserver;

    Socket clientsocket=new Socket("localhost",10000);

    BufferedReader infromuser=new BufferedReader(new InputStreamReader(System.in));
    PrintWriter outtoserver=new PrintWriter(clientsocket.getOutputStream(),true);

    BufferedReader infromserver=new BufferedReader(new InputStreamReader(clientsocket.getInputStream()));

    while((toserver=infromuser.readLine())!=null)
    {
    toserver=infromuser.readLine();
    outtoserver.println(toserver);

    fromserver=infromserver.readLine();
    System.out.println("received"+fromserver);


    }
clientsocket.close();
    }
}

TCPServer.java

import java.io.*;
import java.net.*;

class TCPServer
{
    public static void main(String ar[]) throws Exception
    {
    String toclient;
    String fromclient;

    ServerSocket server=new ServerSocket(10000);
    System.out.println("Waiting for client on 10000");


    Socket connected=server.accept();
    System.out.println("connection succ");

    BufferedReader infromuser=new BufferedReader(new InputStreamReader(System.in));
    PrintWriter outtoclient=new PrintWriter(connected.getOutputStream(),true);
    BufferedReader infromclient=new BufferedReader(new InputStreamReader(connected.getInputStream()));


        while((toclient=infromuser.readLine())!=null)
        {

        fromclient=infromclient.readLine();
        System.out.println("received from client"+fromclient);
        toclient=infromuser.readLine();
        outtoclient.println(toclient);
        }


connected.close();


    }
}

I dont know the problem.
It is not subsequently chat with server.This is simple client server chat application.

The server seems to be waiting for input from its local user before it tries to read iput from the remote client. That seems wrong. Line 23 may be the problem.
ps Next time please describe your problem properly - eg what results you expect vs what you got. Saying " it is not working in proper way" isn't good enough.

Comments
thanks

Thanks james....

It doesn't communicate in two way like client server chat.
Client send something to server and server replies on that to client.
Some strings reached at server but some are not.
i don't know where's the problem in two way communication.

client and server both can read/write to each other.
But this is not happening in my program.

now please tell me what is the problem.

You are using buffered streams, so you may need to flush() the output streams after you write each line to them. (If the output buffer is not full the data will not be transmitted until another write fills the buffer OR you flush it explicitly.)

Edited 3 Years Ago by JamesCherrill

i have already set Autoflush true at line 14 and 19.still it doesn't give perfect output....

means its not synchronized chat application.
Only one can send after other have received.simultaneously both sender and reciever can't send message.
That's mean of perfect output.

OK. To allow send and receive independently/simultaneously you need two threads.
Once you have got a connection you start a new thread which opens the input stream and sits in a loop waiting for input and processing it. The original thread opens the output stream and handles any output requests.

This article has been dead for over six months. Start a new discussion instead.