Reputation: 2809
Here is my tcpClient class:
import java.io.*;
import java.net.*;
public class tcpClient
{
private String _ip;
private int _port;
private Socket _clientSocket;
public tcpClient(String IP, int Port)
{
_ip = IP;
_port = Port;
}
public boolean createSocket()
{
boolean retSt = false;
try
{
_clientSocket = new Socket(this._ip, this._port);
retSt = true;
}
catch (UnknownHostException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
return retSt;
}
public boolean disposeSocket()
{
boolean retSt = false;
try
{
_clientSocket.shutdownInput();
_clientSocket.shutdownOutput();
_clientSocket.close();
retSt = true;
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
return retSt;
}
public boolean isConnected()
{
return _clientSocket.isConnected();
}
}
Here is my main method:
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.util.Date;
public class cos
{
public static void main(String[] args)
{
tcpClient client = new tcpClient("192.168.10.39", 1234);
client.createSocket();
System.out.println(client.isConnected());
client.disposeSocket();
System.out.println(client.isConnected());
client.createSocket();
System.out.println(client.isConnected());
client.disposeSocket();
System.out.println(client.isConnected());
}
}
Here is my console output:
true
true
true
true
Why couldn't i disconnect from server?
Upvotes: 0
Views: 45