We're a community of 1077K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,076,174 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

Use of Sockets

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.

4
Contributors
6
Replies
22 Hours
Discussion Span
3 Months Ago
Last Updated
26
Views
Question
Answered
adikimicky
Light Poster
37 posts since Feb 2013
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0

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

JamesCherrill
... trying to help
Moderator
8,516 posts since Apr 2008
Reputation Points: 2,583
Solved Threads: 1,455
Skill Endorsements: 30

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.

M4trixSh4d0w
Light Poster
47 posts since Feb 2013
Reputation Points: 13
Solved Threads: 6
Skill Endorsements: 0

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
radhakrishna.p
Posting Whiz in Training
262 posts since Nov 2012
Reputation Points: 29
Solved Threads: 59
Skill Endorsements: 10

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)
adikimicky
Light Poster
37 posts since Feb 2013
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0

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.

JamesCherrill
... trying to help
Moderator
8,516 posts since Apr 2008
Reputation Points: 2,583
Solved Threads: 1,455
Skill Endorsements: 30

yeah..thanks a lot... :)

adikimicky
Light Poster
37 posts since Feb 2013
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0
Question Answered as of 3 Months Ago by JamesCherrill, radhakrishna.p and M4trixSh4d0w

This question has already been solved: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
 
© 2013 DaniWeb® LLC
Page rendered in 0.0750 seconds using 2.67MB