The connect method initiates a connection on a socket.
int connect (int s, const struct sockaddr *name, socklen_t namelen);
The s argument is a socket. If it is of type SOCK_DGRAM, this call specifies the peer with which the socket is to be associated; this address is that to which datagrams are to be sent, and the only address from which datagrams are to be received.
If the socket is of type SOCK_STREAM, this call attempts to make a connection to another socket. The other socket is specified by name, which is an address in the communications space of the socket. Each communications space interprets the name argument in its own way. Generally, stream sockets may successfully connect only once; datagram sockets may use connect multiple times to change their association. Datagram sockets may dissolve the association by connecting to an invalid address, such as a null address.
The connect() function returns the value 0 if successful; otherwise the value -1 is returned and the global variable errno is set to indicate the error.
Following code snippet shows the usage of connect method:
#include <sys/socket.h> #include <netinet/in.h> #include <unistd.h> void Connect() { struct sockaddr_in serv_addr; int sock_fd; serv_addr.sin_family = AF_INET; serv_addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK); serv_addr.sin_port = htons(5000); sock_fd = socket(AF_INET,SOCK_STREAM,IPPROTO_TCP); connect(sock_fd,(struct sockaddr*)&serv_addr,sizeof(serv_addr)); close(sock_fd); }
| Thread | Thread Starter | Forum | Replies | Last Post |
|---|---|---|---|---|
| Sockets in Nokia 3220 | martijn.verdonk | Mobile Java Networking & Messaging & Security | 1 | 2006-01-17 22:04 |
| Getting started with sockets | schnesim | Symbian Networking & Messaging | 4 | 2008-06-27 12:07 |
| Using selectService | yanokwa | Bluetooth Technology | 0 | 2006-12-13 05:28 |
| RSocket::Write | adinkesp | General Symbian C++ | 4 | 2007-06-20 22:28 |
| Sockets | patstr | Symbian Networking & Messaging | 0 | 2002-08-06 09:09 |