Chapter 3 Terminal Specific Function Library
147
connect
Purpose
To initiate a connection on a socket.
Syntax
int connect (SOCKET s, struct sockaddr *name, int namelen);
s
Descriptor identifying a socket
name
Pointer to a sockaddr structure containing the remote IP
address and port number
namelen
Integer specifying the length of
name
Example
SOCKET s;
struct sockaddr_in name;
struct hostent *phostent;
s = socket (PF_INET, SOCK_STREAM, TCP);
if (s < 0) {
printf ("SOCKET allocation failed");
...................
}
memset (&name, 0, &sizeof (name));
name.sin_family = AF_INET;
name.sin_port = htons (3000);
phostent = gethostbyname ("server1.Metrologic.com.tw");
if (!phostent) {
printf ("Can not get IP from DNS server");
...................
}
memcpy (&name.sin_addr, phostent->h_addr_list[0], 4);
if (connect (s, (struct sockaddr*) &name, sizeof(name)) < 0) {
printf ("Error in Establishing connection");
...................
}
Description
This routine establishes a connection to a specified socket.
It performs an active open (client mode), allowing a client application to
establish a connection with a remote server.
When it completes successfully, the socket is ready to send/recv data.
Return
If successful, it returns 0.
On error, it returns -1.
The global variable
errno
is set to indicate the error condition encountered.
Содержание Optimus R
Страница 1: ...Printed on 20 March 2006 C Programming Guide Version 3 04 02 Optimus S Optimus R...
Страница 6: ......
Страница 8: ...2 C Programming Guide For Optimus S R...
Страница 12: ...6 C Programming Guide For Optimus S R The flow is illustrated as shown below...
Страница 23: ...Chapter 2 Development Environment 17 Different types signed unsigned Different types same size...
Страница 24: ...18 C Programming Guide For Optimus S R...
Страница 220: ...214 C Programming Guide For Optimus S R See Also FlashSize free_memory...
Страница 232: ...226 C Programming Guide For Optimus S R 4 11 Implementation defined Limits limits h float h Refer to limit h and float h...
Страница 238: ...232 C Programming Guide For Optimus S R...