- int connect(
- SOCKET s,
- const struct sockaddr* s,
- const struct sockaddr* name,
- int namelen
- );
- if (connect(m_socket,(SOCKADDR*)(&ClientService),sizeof(ClientService)) ==SOCKET_ERROR) {
- cout << "Error on connect" << endl;
- WSACleanup();
- return 1;
- }
- // zWinsockClient.cpp : 定义控制台应用程序的入口点。
- //
- #include "stdafx.h"
- #include <WinSock2.h>
- #include <Windows.h>
- #include <iostream>
- using namespace std;
- int _tmain(int argc, _TCHAR* argv[])
- {
- //Initialize Winsock
- WSAData wsaData;
- int iResult = WSAStartup(MAKEWORD(2,2),&wsaData);
- if (iResult != NO_ERROR) {
- cout << "Error on WSAStartup " <<endl;
- return 1;
- }
- //Creating a socket
- SOCKET m_socket;
- m_socket = socket(AF_INET,SOCK_STREAM,IPPROTO_IP);
- if (m_socket == INVALID_SOCKET) {
- cout << "Error on socket " << WSAGetLastError() << endl;
- return 1;
- }
- sockaddr_in ClientService;
- ClientService.sin_family = AF_INET;
- ClientService.sin_addr.s_addr = inet_addr("127.0.0.1");
- ClientService.sin_port = htons(27002);
- if (connect(m_socket,(SOCKADDR*)(&ClientService),sizeof(ClientService)) ==SOCKET_ERROR) {
- cout << "Error on connect" << endl;
- WSACleanup();
- return 1;
- }
- //Send & receive data
- int nSendBytes;
- int nRecvBytes = SOCKET_ERROR;
- char pSendBuf[32] = "Client:Send data";
- char pRecvBuf[32] = "";
- nSendBytes = send(m_socket,pSendBuf,strlen(pSendBuf),0);
- cout << "Num of Send data: " << nSendBytes << endl;
- /*while (nRecvBytes == 0 ||
- (nRecvBytes == SOCKET_ERROR && WSAGetLastError() == WSAECONNRESET))*/ {
- Sleep(5000);
- nRecvBytes = recv(m_socket,pRecvBuf,32,0);
- if (nRecvBytes == -1) {
- cout << "Connection Close" << endl;
- //break;
- }
- if (nRecvBytes < 0) {
- return 1;
- }
- cout << "Num of Recv data: " << nRecvBytes << endl;
- cout << pRecvBuf << endl;
- }
- WSACleanup();
- return 0;
- }
|
delphi实现的Winsock客户端代码
|
|
声明部分
|
|
|
实现代码
|
|
3368



被折叠的 条评论
为什么被折叠?



