user778900
user778900

Reputation: 301

creating socket connection

I need to create a socket connection between my machine and a server. Then I need to send some sms to server from my mechine using smpp protocol. Now I am not being able to create the socket connection. Can any body help me by giving some code to create the socket connection.

My code is:

 import java.io.IOException;
 import java.net.Socket;

 import com.logica.smpp.TCPIPConnection;

 public class SocketConnection {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub

        SocketConnection tl= new SocketConnection();
        tl.connect();

    }


    public void connect()
    {
        TCPIPConnection tc = new TCPIPConnection("172.16.7.92", 9410);
        try {
            tc.accept();
            System.out.println("connected");

        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
 }

this code is not working.

Thanks,

koushik

Upvotes: 1

Views: 5841

Answers (2)

Vlad
Vlad

Reputation: 10780

Here's a simple example how to open a plain socket (to www.google.com, on port 80/HTTP) and use it to send and read data:

try {
    Socket socket = new Socket("www.google.com", 80);
    PrintWriter writer = new PrintWriter(socket.getOutputStream(), true);
    BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
    writer.println("GET /");
    String line;
    while ((line = reader.readLine()) != null) {
        System.out.println(line);
    }
    socket.close();
} catch (Exception e) {
    e.printStackTrace();
}

For your case there seems to be an open() method for the TCPIPConnection. Probably you should use that instead of accept().

Upvotes: 1

Paul Cager
Paul Cager

Reputation: 1920

If you are trying to connect outwards to a server (rather than listen for incoming connections), then you shouldn't be calling accept.

Upvotes: 1

Related Questions