Hello everyone, I am trying to make a chat application. For this i have made two programs-SERVER and CLIENT.
But these are connecting.

import java.io.*;
import java.net.*;
public class SimpleServer
{
    public static void main(String args[])
    {
        ServerSocket s=null;
        try
        {
            s=new ServerSocket (6666);
        }
        catch(IOException e)
        {
            e.printStackTrace();
        }
        while(true)
        {
            try
            {
                Socket s1=s.accept();
                OutputStream s1out=s1.getOutputStream();
                BufferedWriter bw= new BufferedWriter(new OutputStreamWriter(s1out));
                bw.write("hello");
                bw.close();
                s1.close();
            }
            catch(IOException ex)
            {
                ex.printStackTrace();
            }
        }
    }
}

and Client.java

import java.net.*;
import java.io.*;
public class SimpleClient 
{
    public static void main(String args[])
    {
        try
        {
            Socket s1=new Socket("171.76.149.182",6666);
            InputStream is=s1.getInputStream();
            DataInputStream dis=new DataInputStream(is);
            System.out.println(dis.readUTF());
           // br.close();
            s1.close();
        }
        catch(ConnectException connExc)
        {
            System.err.println("could not connect");
        }
        catch(IOException ex)
        {
            ex.printStackTrace();
        }
        {

        }
    }

}

I am getting the following error:

run:
java.io.EOFException
    at java.io.DataInputStream.readFully(DataInputStream.java:180)
    at java.io.DataInputStream.readUTF(DataInputStream.java:592)
    at java.io.DataInputStream.readUTF(DataInputStream.java:547)
    at SimpleClient.main(SimpleClient.java:12)
BUILD SUCCESSFUL (total time: 0 seconds)

Can anyone please give me their suggestions . Thanks in advance.

You mixed text output and data input streams. They have different formats, and can't be mixed. Either use text streams to send and receive text only, or use a DataOutputStream with a DataInputStream to send and receive different data types (inlcuding UTF Strings).

Also the ip you are using ("171.76.149.182") is your External Ip Address that is the ip used to connect from other computers from outside your network.

You would want to use your Internal Ip Address for connecting to conputers within your local network.

To get the internal ip for windows go into Command Prompt and type ipconfig
then look for IPv4 Address. . . . . . . . . . . : 192.168.X.XX where X.XX are numbers and use that address.

To get the internal ip for Mac/Linux go into Terminal and type ifconfig wan0 and get the ip from there

The internal ip should look something like 192.168.X.XX or 10.X.X.XX

Also, If you want to connect to the same computer use localhost as the ip
that tells it to use the current machine.

hai adikimicky

please do as JamesCherrill said he is right

in order to send and receive data properly between sockets (client and server) we have to use same kind of data flow related classes in java

you used streams for receiving data at Client side but you are sending data to client which is irrelavent data type than at receiving side

replace the following code at your server side code from line no 22 to 24

OutputStream s1out=s1.getOutputStream();
DataOutputStream dos = new DataOutputStream(s1out);
dos.writeUTF("hello");
dos.flush(); // dont forget to call this method 
dos.close();

so that you will get the output as you want

let me know the status

happy coding
suggestion:

dos.flush(); // dont forget to call this method because which confirms that all the data has been sent

By modifying the code I am still getting the error in SimpleServer.java as follows:

run:
java.net.SocketException: Unrecognized Windows Sockets error: 0: JVM_Bind
    at java.net.PlainSocketImpl.socketBind(Native Method)
    at java.net.PlainSocketImpl.bind(PlainSocketImpl.java:359)
    at java.net.ServerSocket.bind(ServerSocket.java:319)
    at java.net.ServerSocket.<init>(ServerSocket.java:185)
    at java.net.ServerSocket.<init>(ServerSocket.java:97)
    at SimpleServer.main(SimpleServer.java:10)
Exception in thread "main" java.lang.NullPointerException
    at SimpleServer.main(SimpleServer.java:20)
Java Result: 1
BUILD SUCCESSFUL (total time: 0 seconds)

It's possible you have a previous test still running, so the port is still in use. Check the task manager for instances of java.exe or javaw.exe.

yeah..thanks a lot... :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.