Winsock API: connect
来源:互联网 发布:ubuntu 解压缩命令 编辑:程序博客网 时间:2024/05/23 13:40
参考MSDN:http://msdn.microsoft.com/en-us/library/windows/desktop/ms737625(v=vs.85).aspx
The connect function establishes a connection to a specified socket.
Syntax
int connect(
_In_ SOCKET s,
_In_ const struct sockaddr *name,
_In_ int namelen
);
Parameters
- s [in]
A descriptor identifying an unconnected socket.
- name [in]
A pointer to the sockaddr structure to which the connection should be established.
- namelen [in]
The length, in bytes, of the sockaddr structure pointed to by the name parameter.
Return value
If no error occurs, connect returns zero. Otherwise, it returns SOCKET_ERROR, and a specific error code can be retrieved by callingWSAGetLastError.
On a blocking socket, the return value indicates success or failure of the connection attempt.
With a nonblocking socket, the connection attempt cannot be completed immediately. In this case,connect will return SOCKET_ERROR, and WSAGetLastError will return WSAEWOULDBLOCK. In this case, there are three possible scenarios:
- Use the select function to determine the completion of the connection request by checking to see if the socket is writeable.
- If the application is using WSAAsyncSelect to indicate interest in connection events, then the application will receive an FD_CONNECT notification indicating that theconnect operation is complete (successfully or not).
- If the application is using WSAEventSelect to indicate interest in connection events, then the associated event object will be signaled indicating that theconnect operation is complete (successfully or not).
Example Code
The following example demonstrates the use of the connect function.
#ifndef UNICODE#define UNICODE#endif#define WIN32_LEAN_AND_MEAN#include <winsock2.h>#include <ws2tcpip.h>#include <stdio.h>// Need to link with Ws2_32.lib#pragma comment(lib, "ws2_32.lib")int wmain(){ //---------------------- // Initialize Winsock WSADATA wsaData; int iResult = WSAStartup(MAKEWORD(2, 2), &wsaData); if (iResult != NO_ERROR) { wprintf(L"WSAStartup function failed with error: %d\n", iResult); return 1; } //---------------------- // Create a SOCKET for connecting to server SOCKET ConnectSocket; ConnectSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); if (ConnectSocket == INVALID_SOCKET) { wprintf(L"socket function failed with error: %ld\n", WSAGetLastError()); WSACleanup(); return 1; } //---------------------- // The sockaddr_in structure specifies the address family, // IP address, and port of the server to be connected to. sockaddr_in clientService; clientService.sin_family = AF_INET; clientService.sin_addr.s_addr = inet_addr("127.0.0.1"); clientService.sin_port = htons(27015); //---------------------- // Connect to server. iResult = connect(ConnectSocket, (SOCKADDR *) & clientService, sizeof (clientService)); if (iResult == SOCKET_ERROR) { wprintf(L"connect function failed with error: %ld\n", WSAGetLastError()); iResult = closesocket(ConnectSocket); if (iResult == SOCKET_ERROR) wprintf(L"closesocket function failed with error: %ld\n", WSAGetLastError()); WSACleanup(); return 1; } wprintf(L"Connected to server.\n"); iResult = closesocket(ConnectSocket); if (iResult == SOCKET_ERROR) { wprintf(L"closesocket function failed with error: %ld\n", WSAGetLastError()); WSACleanup(); return 1; } WSACleanup(); return 0;}
- Winsock API: connect
- Winsock基本的api
- Winsock API TCPServer
- Winsock API 编程介绍
- WinSock之基础API
- Winsock API 函数大全
- WINSOCK API 学习笔记
- Winsock API 编程介绍
- Winsock API 编程介绍
- 1.WINSOCK API
- WinSock API 入门
- WinSock API 常用函数
- Winsock API 编程介绍
- Winsock API 函数大全
- Winsock API 函数大全
- winsock api编程
- Winsock API 函数大全
- Winsock API: WSAStartup
- Xcode 4的Build目录位置
- "_kCMTimeInvalid", referenced from 错误原因
- PHP中可变变量到底有什么用?
- 华为,你将被谁抛弃——华为十大内耗问题浅析
- ReportViewer报表导出成Excel,Image,PDF---LocalReport.Render
- Winsock API: connect
- Cuda stream programming
- java 使用rxtx 进行串口交流。
- SDL 1.3 on iOS
- 建网站
- Android Service获得手机信息:
- ARM-Linux配置DHCP自动获取IP地址
- 使用存储过程实现显示搜索结果和数据条数
- 第一次C程序上机实验报告