1,105,585 Community Members

Use of Sockets

Member Avatar
adikimicky
Light Poster
37 posts since Feb 2013
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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.

Member Avatar
JamesCherrill
... trying to help
10,393 posts since Apr 2008
Reputation Points: 2,094 [?]
Q&As Helped to Solve: 1,752 [?]
Skill Endorsements: 47 [?]
Moderator
Featured
 
1
 

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).

Member Avatar
M4trixSh4d0w
Light Poster
47 posts since Feb 2013
Reputation Points: 13 [?]
Q&As Helped to Solve: 6 [?]
Skill Endorsements: 0 [?]
 
2
 

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.

Member Avatar
radhakrishna.p
Posting Whiz in Training
281 posts since Nov 2012
Reputation Points: 29 [?]
Q&As Helped to Solve: 64 [?]
Skill Endorsements: 11 [?]
 
1
 

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
Member Avatar
adikimicky
Light Poster
37 posts since Feb 2013
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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)
Member Avatar
JamesCherrill
... trying to help
10,393 posts since Apr 2008
Reputation Points: 2,094 [?]
Q&As Helped to Solve: 1,752 [?]
Skill Endorsements: 47 [?]
Moderator
Featured
 
2
 

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.

Member Avatar
adikimicky
Light Poster
37 posts since Feb 2013
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

yeah..thanks a lot... :)

Question Answered as of 1 Year Ago by JamesCherrill, radhakrishna.p and M4trixSh4d0w
You
This question has already been solved: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article