My clas I created doesn't seem to send data to the server :(
Hello everyone.
My mentor wanted me to write a class that would do the following:
open a connection to the passed in port and host
send a message
receive a message
close the connection
Sounds easy enough and I wrote a program that does just that but when I put it into a class it stopped working.
Here's my class that I thought would do all of that, it seems to open a connection successfully but the output server I connect to never gets any of the messages and no exceptions are thrown.
import java.net.*;
import java.io.*;
import java.lang.Exception;
publicclass SocketConnect{
Socket cSocket =null;;
Message msg =null;
String host;
int port;
public SocketConnect(Socket clientSocket, String host,int port)
throws IOException, UnknownHostException{
cSocket = clientSocket;
this.host = host;
this.port = port;
}
publicvoid socketSend(Message msg){
BufferedWriter out =null;
BufferedReader in =null;
// open up connection with server
try{
cSocket =new Socket(host, port);
System.out.println("Connected to: " + cSocket);
// setting up output stream to server
out =new BufferedWriter(new OutputStreamWriter(cSocket
.getOutputStream()));
// setting up input stream to server
in =new BufferedReader(new InputStreamReader(cSocket
.getInputStream()));
}catch (UnknownHostException e)
{
System.out.println("Unknown Host: " + e);
}catch (IOException ioEE)
{
System.out.println("IOException: " + ioEE);
}
try{
//cSocket.setSoTimeout(5000);// time out in 25 seconds
// sending data to server
out.write(msg.toString()+'\n');
//System.out.println("Waiting for ACK from Output Server...");
// waiting to get Acknowledgement from the server
String line;
while ((line = in.readLine()) !=null)// If we get a line
{
System.out.println(line);
}
// else its going to throw an exception which will be
// caught outside this function.
}catch (SocketTimeoutException sE){
System.out.println("Didn't get Ack from server, trying again..");
socketSend(msg);
}catch (IOException e){
System.out.println("IOException : " + e);
}finally{
try{
in.close();
out.close();
cSocket.close();
}catch (IOException ioE){
System.out.println("IOEXception: " + ioE);
}
}
}
}
I'm using my class in a thread for instance, everytime a client connects to the server it will spawn a new thread and I use it like this:
publicvoid run(){
//setting up sockets
Socket outputServ =null;
SocketConnect sConnect =null;
//Setting up SocketConnect object
try
{
sConnect =new SocketConnect(outputServ,"localhost", 2222);
}
catch(UnknownHostException a)
{
System.out.println("Unknown Host: " + a);
}
catch(IOException b)
{
System.out.println("IOException: " + b);
}
//setting up Message object to later be populated with string
Message msg =null;
BufferedReader in =null;
....
....
//sending the string version of the message object to the
//output server
sConnect.socketSend(msg);
....
}
that sConnect.socketSend(msg), will send the string version of the message, and it gets all the way inside the function right before its about to send to the server running on port 2222 named "localhost" but localhost never gets it. But the server running on 2222 gets the incoming connection, becuase it says, my server application has connected to it.
Any ideas would be great!
PS: the server I connect to on port 2222 hostname: localhost does work, I tried it with my other program and I can send messages to it, and it will display my message I sent it.

