sanchop22
sanchop22

Reputation: 2809

could not disconnect socket

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

Answers (1)

vandale
vandale

Reputation: 3650

from java doc:

Note: Closing a socket doesn't clear its connection state, which means this method will return true for a closed socket (see isClosed()) if it was successfuly connected prior to being closed.

Upvotes: 1

Related Questions