Accessing DataOutputStream
I have a class named Class1 and it has a boolean method isClientConnection(InetAddress address, int serverPort) and code is
boolean isClientConnection(InetAddress address, int serverPort) {
//Here address is localhost and serverPort is 4093
InetAddress serverName = address;
int port = serverPort;
try {
System.out.println("Connecting to " + serverName
+ " on port " + port);
client = new Socket(serverName, port);
System.out.println("Just connected to "+ client.getRemoteSocketAddress());
outToServer = client.getOutputStream();
out = new DataOutputStream(outToServer);
out.writeUTF("OIE Raju " + client.getLocalSocketAddress());
out.flush();
return true;
} catch (IOException e) {
return false;
}
}
what i want is that i want to access out object from another class named Class2.
I tried this under Class2 but didnt work:
Class1 c1=new Class1();
c1.out.writeBytes("hellowwww");
How can i solve it ???? Help !!
Re: Accessing DataOutputStream
Please explain. You need to show the definition of Class1 where out is defined and given a value.
Re: Accessing DataOutputStream
Oh sorry for not providing it. I have made them global variable as :
Socket client;
OutputStream outToServer;
DataOutputStream out;
Re: Accessing DataOutputStream
Are the variables local to a method or are they class variables? Are they public or private?
Please post the full text of any error messages.
Re: Accessing DataOutputStream
under class2.java, i write :
private void sendButtonActionPerformed(java.awt.event.ActionEve nt evt) {
Class1 c1=new Class1();
try {
mf.out.writeUTF("Hellooooooooo");
} catch (IOException ex) {
System.out.println("unable to send message");
}
but it didnt work.
Helllooooooooo is not written to the serverPort (4093) that i defined in Class1.
Sorry for my weak English.
Is mf.out.writeUTF("Helloooooooo") wrong ??
}
Re: Accessing DataOutputStream
Quote:
but it didnt work.
Quote:
Helllooooooooo is not written
What is written?
You will have to provide a small, complete program that compiles and executes to test with. A SSCCE
Re: Accessing DataOutputStream
That code there instantiates c1 and then doesn't use it.
How does the call to writeUTF() have anything to do with that Class1 object?