// TcpSocket_Server.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include <windows.h>
#include <iostream>
using namespace std;
#pragma comment(lib, "Ws2_32.lib")
int _tmain(int argc, _TCHAR* argv[])
{
int errInfo;
WSADATA wsadata;
WORD version = MAKEWORD(1,1);
errInfo = WSAStartup(version,&wsadata);
if(errInfo != NOERROR)
{
printf_s("socket init error code = %d",errInfo);
return 0;
}
int _socket = socket(AF_INET,SOCK_STREAM,0);
sockaddr_in _sockaddr_in;
_sockaddr_in.sin_family = AF_INET;
_sockaddr_in.sin_addr.S_un.S_addr = htonl(INADDR_ANY);
_sockaddr_in.sin_port = htons(8972);
bind(_socket,(sockaddr*)&_sockaddr_in,sizeof(_sockaddr_in));
listen(_socket,5);
sockaddr_in client_sin;
int len = sizeof(client_sin);
int client_socket = accept(_socket,(sockaddr*)&client_sin,&len);
char recv_buf[1024];
recv(client_socket,recv_buf,sizeof(recv_buf),0);
printf_s("data is %s",recv_buf);
char send_buf[1024] = {"这里是服务端\n"};
send(client_socket,send_buf,sizeof(send_buf),0);
closesocket(_socket);
system("pause");
return 0;
}
网友评论