1

I am trying to program a TCP chat server, but am having difficulties with the .getInputStream() and .getOutputStream() methods, the compiler says it "Cannot find symbol- method .getInputStream(). Here is my code, I have not progressed very far yet:

import java.net.*;
import java.io.*;
public class Server {
 public static void Server (String[] args) {
  ServerSocket SS1 = null;
  DataOutputStream DOS1 = null;
  DataInputStream DIS1 = null; //Setting the values to null
  try {
   SS1 = new ServerSocket(5000); //setting the socket SS1 to port 5000 and creating an instance
   Socket clientSocket = SS1.accept(); //accepting the connection request
   DOS1 = new DataOutputStream(SS1.getOutputStream());
   DIS1 = new DataInputStream(SS1.getInputStream()); //creating output and input streams
  }
  catch (Exception e){
   System.err.println("Error!");
  }
 }
}

I am using BlueJ on Windows 7, if that's the problem. Also, I am can't seem to find good explanations about how data streams or "old-school" sockets work, so if anyone knows where I can get those, it'd be very much appreciated. :)

~Alon.

pnuts
  • 58,317
  • 11
  • 87
  • 139
Alon and Idan
  • 87
  • 1
  • 10
  • you are using `ServerSocket` you should be doing `clientSocket.get****()` They belong to the `Socket` class not `ServerSocket` – 3kings Oct 22 '15 at 15:00

2 Answers2

1

You have to call:

clientSocket.getOutputStream()
clientSocket.getInputStream()

inside your DataOutput-/DataInputStream constructors.

masinger
  • 775
  • 6
  • 13
  • So, like this? DOS1 = new DataOutputStream(clientSocket.getOutputStream()); DIS1 = new DataInputStream(clientSocket.getInputStream()); – Alon and Idan Oct 24 '15 at 07:01
1

You are mistakenly using the ServerSocket inside of the Socket (the client connecting to the server)

Try this:

clientSocket.getOutputStream() clientSocket.getInputStream()

3kings
  • 838
  • 2
  • 13
  • 28
  • Thanks. I tried adding `if ((DIS1 != null) && (DOS1 != null) && (SS1 != null)){ System.out.println("Connection is not- null."); } else { System.out.println("Connection is null!"); }` as well, but it does not seem to do anything besides taking input with no exception. What is going on? Also, now I'm trying to create a client, and `getOutputStream()` gives the compiler error of "non-static method getOutputStream() cannot be referenced from a static context" . What do I do now? – Alon and Idan Oct 28 '15 at 12:46
  • @AlonandIdan you don't so much need to check for null because it will throw an `exception` if anything goes wrong. They should never and will never be null. – 3kings Oct 28 '15 at 13:17
  • @AlonandIdan now `getOutputStream()` is a method of the `Socket` class so you need to call it with an object of that type. So in the above code you would need to do `clientSocket.getOutputStream()` and that will work fine. As you see `clientSocket` is a `Socket` object – 3kings Oct 28 '15 at 13:19
  • I just want to know if there _is_ a connection at all before I continue my code. – Alon and Idan Oct 29 '15 at 10:11
  • 1
    So do I neet to use `clientSocket.getOutputStream()` both in he server _and_ the client? I tried it and it is giving `cannot find symbol- variable clientSocket`. – Alon and Idan Oct 29 '15 at 10:12
  • @AlonandIdan well you need to use whatever the variable name is in your Client class. `Socket what? = new Socket("ip",port);` call it with the socket you are creating to connect to the server. – 3kings Oct 29 '15 at 14:33