1、服务器
// vs_demo.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <Windows.h>
#include <WinSock2.h>
#pragma comment(lib, "ws2_32.lib")
#define LISTEN_PORT 5000
#define BUFFER_SIZE 1024
int server_daemon(){
WSADATA wsd;
SOCKET sock_listen;
int rc;
if(WSAStartup(MAKEWORD(2,2), &wsd) != 0){
printf("error:WSAStartup failed!\n");
return -1;
}
//create socket
sock_listen = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if(INVALID_SOCKET == sock_listen){
printf("error:socket failed!\n");
WSACleanup();
return -2;
}
//set socket address
sockaddr_in local_addr;
local_addr.sin_family = AF_INET;
local_addr.sin_port = htons(LISTEN_PORT);
local_addr.sin_addr.s_addr = INADDR_ANY;
//bind
rc = bind(sock_listen, (struct sockaddr*)&local_addr, sizeof(sockaddr_in));
if(SOCKET_ERROR == rc){
printf("error:bind failed!\n");
closesocket(sock_listen);
WSACleanup();
return -3;
}
//listen
rc = listen(sock_listen, 5);
if(SOCKET_ERROR == rc){
printf("error:listen failed!\n");
closesocket(sock_listen);
WSACleanup();
return -4;
}
//accept client's connection
sockaddr_in remote_addr;
int remote_len = sizeof(remote_addr);
SOCKET sock_conn = accept(sock_listen, (struct sockaddr*)&remote_addr, &remote_len);
if(INVALID_SOCKET == rc){
printf("error: accept failed!\n");
closesocket(sock_listen);
WSACleanup();
return -5;
}
//recv data
char buffer[BUFFER_SIZE] = {0};
rc = recv(sock_conn, buffer, BUFFER_SIZE, 0);
char buffer1[BUFFER_SIZE] = {0};
rc = recv(sock_conn, buffer1, BUFFER_SIZE, 0);//客户端调用多少次send,服务器端调用多少次的recv
if(SOCKET_ERROR == rc){
printf("error:recv failed!\n");
closesocket(sock_listen);
closesocket(sock_conn);
WSACleanup();
return -6;
}
printf("[recv]: %s\n", buffer);
//exit
closesocket(sock_listen);
closesocket(sock_conn);
WSACleanup();
return 0;
}
int _tmain(int argc, _TCHAR* argv[])
{
server_daemon();
getchar();
return 0;
}
2、客户端
// vs_demo.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <Windows.h>
#include <WinSock2.h>
#pragma comment(lib, "ws2_32.lib")
#define LISTEN_PORT 5000
#define BUFFER_SIZE 1024
int client_daemon(){
WSADATA wsd;
if(WSAStartup(MAKEWORD(2,2),&wsd) !=0){
printf("error:WSAStartup failed!\n");
return -1;
}
SOCKET sock_conn;
sock_conn = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if(INVALID_SOCKET == sock_conn){
printf("error:socket failed!\n");
WSACleanup();
return -2;
}
//server address
sockaddr_in server_addr;
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(LISTEN_PORT);
server_addr.sin_addr.s_addr = inet_addr("127.0.0.1");
//connect
int rc = connect(sock_conn, (struct sockaddr*)&server_addr, sizeof(server_addr));
if(SOCKET_ERROR == rc){
printf("error:connect failed!\n");
closesocket(sock_conn);
WSACleanup();
return -3;
}
//send data
char buffer[BUFFER_SIZE] = {0};
strcpy_s(buffer, sizeof(buffer), "Hello,Server!");
rc = send(sock_conn, buffer,(int) strlen(buffer), 0);
char buffer1[BUFFER_SIZE] = {0};
strcpy_s(buffer1, sizeof(buffer1), "this is tcp data!");
rc = send(sock_conn, buffer1,(int) strlen(buffer1), 0);
if(SOCKET_ERROR == rc){
printf("error:send failed!\n");
closesocket(sock_conn);
WSACleanup();
return -4;
}
printf("[send]: %s\n",buffer);
//close socket
closesocket(sock_conn);
WSACleanup();
return 0;
}
int _tmain(int argc, _TCHAR* argv[])
{
client_daemon();
getchar();
return 0;
}