0

Hey guys I'm trying to write a simple socket program that basically send like a "Hello" message from client and then server gets and print its out.

I'm trying to follow this guide: http://www.javaworld.com/jw-12-1996/jw-12-sockets.html?page=4

However when i try to instantiate serverSocket with port number it causes syntax error that advises to either remove the argument or create a new constructor for that method. It also doesn't recognize accept() method when I try to use it. Anyone know why this is happening?

Here is my Client code:

public static void main(String[] args) throws UnknownHostException, IOException 
{
    Socket testSocket = null;
    DataOutputStream os = null;
    DataInputStream is = null;

    try
    {
        testSocket = new Socket("192.168.0.104", 5932);
        os = new DataOutputStream(testSocket.getOutputStream());
        is = new DataInputStream(testSocket.getInputStream());
    }
    catch (UnknownHostException e)
    {
        System.err.println("Couldn't find Host");
    }
    catch (IOException e)
    {
        System.err.println("Couldn't get I/O connection");
    }

    if (testSocket != null && os != null && is != null)
    {
        try
        {
            os.writeBytes("Hello Server!\n");

            os.close();
            is.close();
            testSocket.close();

        }

        catch (UnknownHostException e)
        {
            System.err.println("Host not found");
        }
        catch (IOException e)
        {
            System.err.println("I/O Error");
        }
    }

}

Here is my Server Code (UPDATED):

public static void main(String[] args) 
{
    String line = new String() ;


    try
    {
       ServerSocket echoServer = new ServerSocket(5932);

       Socket clientSocket = echoServer.accept();

      DataInputStream  is = new DataInputStream(clientSocket.getInputStream());
      PrintStream os = new PrintStream(clientSocket.getOutputStream());

           while (true) {
             line = is.readLine();
             os.println(line); 
           }
    }
    catch (IOException e)
    {
        System.out.println(e.getMessage());
    }

}

Here are actual screenshots.

Error1: https://i.stack.imgur.com/oBKZt.png Error2: https://i.stack.imgur.com/HcydP.png

Nick
  • 597
  • 3
  • 14
  • 34
  • 2
    The guide you are referencing is from 1996. I would be surprised if that code worked with the latest versions of Java. – kurtzbot Aug 30 '12 at 17:14
  • See also this [example](http://stackoverflow.com/a/3245805/230513). – trashgod Aug 30 '12 at 17:15
  • The Java docs themselves have a very good [tutorial](http://docs.oracle.com/javase/tutorial/networking/index.html) for networking in java. I would recommend that than the guide you are currently using. – kurtzbot Aug 30 '12 at 17:16
  • Try using another port number, such as 44444. Some ports are reserved. – squiguy Aug 30 '12 at 17:25

4 Answers4

2

Your class is named ServerSocket. It doesn't have a constructor that takes an int. Name your class something else so it doesn't conflict with java.net.ServerSocket.

Either that use the absolute path

java.net.ServerSocket clientSocket = new java.net.ServerSocket(5932)
km1
  • 2,383
  • 1
  • 22
  • 27
1

You have not posted import statements. Classes ServerSocket, Socket etc belong to package java.net. Double check that you have import statements in the beginning of your class.

something like import java.net*. Or, better use IDE to help you. Ctrl-Shift-O in Eclipse will do the work.

AlexR
  • 114,158
  • 16
  • 130
  • 208
  • Your explanation is not clear. It seems you have compilation problem. If your environment is OK it should not be. Is it a chance that you have other imports that cause compiler to take `ServerSocket` from other package? – AlexR Aug 30 '12 at 17:27
1

EDIT BASED ON SCREEN SHOTS:
The class you defined is named ServerSocket. This is hiding the actual class you are trying to use. Your class does not implement a constructor that takes an int and does not define accept(), these are the errors you are receiving. You should not name your class the same name as another existing class as it leads to these types of errors and further confusion.

ORIGINAL:
You have an syntax error when you handle the exception thrown by the ServerSocket.

try
{
    echoServer = new ServerSocket(5932);
}
catch (IOException e)
{
    System.out
}

It looks like you are missing .println(e); from the end of the last line.

unholysampler
  • 17,141
  • 7
  • 47
  • 64
1

Try it this way....

- First check that you have properly imported the package java.net.*

public static void main(String[] args) 
{

    String line = new String() ;


    try
    {
       ServerSocket echoServer = new ServerSocket(5932);

       Socket clientSocket = echoServer.accept();

      DataInputStream  is = new DataInputStream(clientSocket.getInputStream());
      PrintStream os = new PrintStream(clientSocket.getOutputStream());

           while (true) {
             line = is.readLine();
             os.println(line); 
           }
    }
    catch (IOException e)
    {
        System.out
    }



}
Kumar Vivek Mitra
  • 33,294
  • 6
  • 48
  • 75