Code/Resource
Windows Develop
Linux-Unix program
Internet-Socket-Network
Web Server
Browser Client
Ftp Server
Ftp Client
Browser Plugins
Proxy Server
Email Server
Email Client
WEB Mail
Firewall-Security
Telnet Server
Telnet Client
ICQ-IM-Chat
Search Engine
Sniffer Package capture
Remote Control
xml-soap-webservice
P2P
WEB(ASP,PHP,...)
TCP/IP Stack
SNMP
Grid Computing
SilverLight
DNS
Cluster Service
Network Security
Communication-Mobile
Game Program
Editor
Multimedia program
Graph program
Compiler program
Compress-Decompress algrithms
Crypt_Decrypt algrithms
Mathimatics-Numerical algorithms
MultiLanguage
Disk/Storage
Java Develop
assembly language
Applications
Other systems
Database system
Embeded-SCM Develop
FlashMX/Flex
source in ebook
Delphi VCL
OS Develop
MiddleWare
MPI
MacOS develop
LabView
ELanguage
Software/Tools
E-Books
Artical/Document
Client.cpp
Package: winsock.rar [view]
Upload User: whzytd4
Upload Date: 2022-08-01
Package Size: 7346k
Code Size: 3k
Category:
WinSock-NDIS
Development Platform:
Visual C++
- // Client.cpp : Defines the entry point for the console application.
- //
- #include "stdafx.h"
- #include <iostream.h>
- #include "winsock2.h"
- #pragma comment(lib, "WS2_32.lib")
- #define MAX_NUM_BUF 64
- char bufRecv[MAX_NUM_BUF];
- char bufSend[MAX_NUM_BUF];
- SOCKET sHost;
- bool bConning;
- WORD wVersionRequested;
- WSADATA wsd;
- int retVal;
- void InitMember(void);
- void ShowErrorMsg(void);
- bool RecvLine(SOCKET s, char* buf);
- void InitMember()
- {
- memset(bufRecv, 0, MAX_NUM_BUF);
- memset(bufSend, 0, MAX_NUM_BUF);
- sHost = INVALID_SOCKET;
- bConning = false;
- }
- void ShowErrorMsg()
- {
- int Error = GetLastError();
- HLOCAL hlocal = NULL;
- bool fok = FormatMessage(
- FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_ALLOCATE_BUFFER,
- NULL, Error, MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US),
- (PTSTR)&hlocal, 0, NULL);
- if (hlocal != NULL)
- {
- MessageBox(NULL, (char*)LocalLock(hlocal), "CLIENT ERROR", MB_OK);
- LocalFree(hlocal);
- }
- }
- bool RecvLine(SOCKET s, char* buf)
- {
- bool retVal = TRUE; //返回值
- bool bLineEnd = FALSE; //行结束
- int nReadLen = 0; //读入字节数
- int nDataLen = 0; //数据长度
- while (!bLineEnd && bConning) //与客户端连接 没有换行
- {
- nReadLen = recv(s, buf + nDataLen, 1, 0);//每次接收一个字节
- //错误处理
- if (SOCKET_ERROR == nReadLen)
- {
- retVal= FALSE; //读数据失败
- break; //跳出循环
- }
- if (0 == nReadLen)//客户端关闭
- {
- retVal = FALSE; //读数据失败
- break ; //跳出循环
- }
- //读入数据
- if ('n' == *(buf + nDataLen)) //换行符
- {
- bLineEnd = TRUE; //接收数据结束
- }else{
- nDataLen += nReadLen; //增加数据长度
- }
- }
- return retVal;
- }
- int main(int argc, char* argv[])
- {
- InitMember();
- wVersionRequested = MAKEWORD(2, 2);
- int err = WSAStartup(wVersionRequested, &wsd);
- if(err != 0)
- {
- MessageBox(NULL, "can not find a usable Windows Sockets dll!", "ERROR", MB_OK);
- return -1;
- }
- sHost = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
- if(sHost == INVALID_SOCKET)
- {
- ShowErrorMsg();
- WSACleanup();
- return -1;
- }
- cout<<"Client Succeeded"<<endl;
- cout<<"Be ready to connect to server..."<<endl;
- LPHOSTENT hostEntry;
- char hostname[MAX_NUM_BUF];
- gethostname(hostname, MAX_NUM_BUF);
- hostEntry = gethostbyname(hostname);
- if(!hostEntry)
- {
- ShowErrorMsg();
- return -1;
- }
- SOCKADDR_IN addrServ;
- addrServ.sin_family = AF_INET;
- addrServ.sin_addr = *((LPIN_ADDR)*hostEntry->h_addr_list);
- addrServ.sin_port = htons(5500);
- retVal = connect(sHost, (SOCKADDR *)&addrServ, sizeof(SOCKADDR_IN));
- if(retVal == SOCKET_ERROR)
- {
- ShowErrorMsg();
- return -1;
- }else
- {
- bConning = true;
- }
- cout<<"Connect successfully!"<<endl;
- strcpy(bufSend, "Hello, Server!n");
- retVal = send(sHost, bufSend, strlen(bufSend), 0);
- if(retVal == SOCKET_ERROR)
- {
- ShowErrorMsg();
- return -1;
- }
- if (!RecvLine(sHost, bufRecv))
- {
- ShowErrorMsg();
- return -1;
- }
- cout<<bufRecv<<endl;
- closesocket(sHost);
- WSACleanup();
- cout << "Client exiting..." << endl;
- Sleep(20000);
- }