码迷,mamicode.com
首页 > 其他好文 > 详细

TCP Socket 通讯(客户端与服务端)

时间:2016-10-05 12:55:58      阅读:176      评论:0      收藏:0      [点我收藏+]

标签:

/*----------------------------编译环境:VS2015---------------------------------------*/

/*-----------------------------Author:刘收获-------------------------------------------*/

 

//Sever端口

// ConsoleApplication1.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include <WinSock2.h>
#include <iostream>
#include <Windows.h>
#define PORT 8087
#define SERVER_IP "127.0.0.1"
#define BUFFER_SIZE 1024
#define MESSAGE_MAX_SIZE 512
#pragma comment(lib, "WS2_32") //动态库的静态加载

using namespace std;

int main()
{
//初始化网卡
sockaddr_in server_addr;
server_addr.sin_family = AF_INET;
server_addr.sin_addr.S_un.S_addr = INADDR_ANY;
server_addr.sin_port = htons(PORT);

// 初始化socket dll
WSADATA wsaData;
WORD socketVersion = MAKEWORD(2, 0);
if (WSAStartup(socketVersion, &wsaData) != 0)
{

printf("Init socket dll error!");
exit(1);

}

// 创建socket
SOCKET m_Socket = socket(AF_INET, SOCK_STREAM, 0);
if (SOCKET_ERROR == m_Socket)
{

printf("Create Socket Error!");
exit(1);

}

//绑定socket和服务端(本地)地址
if (SOCKET_ERROR == bind(m_Socket, (LPSOCKADDR)&server_addr, sizeof(server_addr)))
{

printf("Server Bind Failed: %d", WSAGetLastError());
exit(1);

}

//监听
if (SOCKET_ERROR == listen(m_Socket, 10))
{

printf("Server Listen Failed: %d", WSAGetLastError()); //失败
exit(1);

}

while (1) //等待客户端连接
{

printf("Listening To Client.\n");
sockaddr_in client_addr;
int client_addr_len = sizeof(client_addr);
SOCKET m_New_Socket = accept(m_Socket, (sockaddr *)&client_addr, &client_addr_len);
if (SOCKET_ERROR == m_New_Socket)
{

printf("Server Accept Failed: %d", WSAGetLastError());
break;

}
char buffer[BUFFER_SIZE];
memset(buffer, 0, BUFFER_SIZE);
if (recv(m_New_Socket, buffer, BUFFER_SIZE, 0) < 0)
{

printf("Server Receive Data Failed!");
break;

}
char message[MESSAGE_MAX_SIZE + 1];
memset(message, 0, MESSAGE_MAX_SIZE + 1);
strncpy_s(message, buffer, strlen(buffer)>MESSAGE_MAX_SIZE ? MESSAGE_MAX_SIZE : strlen(buffer));
printf("%s\n", message);

closesocket(m_New_Socket);

}
closesocket(m_Socket);
//释放winsock库
WSACleanup();

return 0;
}

 

 

//Client端

// ConsoleApplication2.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include <WinSock2.h>
#include <WS2tcpip.h>
#include <iostream>
#include <Windows.h>
#define PORT 8087
#define SERVER_IP "127.0.0.1"
#define BUFFER_SIZE 1024
#define MESSAGE_MAX_SIZE 512
#pragma comment(lib, "WS2_32") //动态库的静态加载
#define _WINSOCK_DEPRECATED_NO_WARNINGS
using namespace std;

int main()
{
// 初始化socket dll
WSADATA wsaData;
WORD socketVersion = MAKEWORD(2, 0);
if (WSAStartup(socketVersion, &wsaData) != 0)
{

printf("Init socket dll error!");
exit(1);

}

//创建socket
SOCKET c_Socket = socket(AF_INET, SOCK_STREAM, 0);
if (SOCKET_ERROR == c_Socket)
{

printf("Create Socket Error!");
system("pause");
exit(1);

}

//指定服务端的地址
sockaddr_in server_addr;
server_addr.sin_family = AF_INET;
// server_addr.sin_addr.S_un.S_addr = inet_addr(SERVER_IP);
inet_pton(AF_INET, SERVER_IP, &server_addr.sin_addr.S_un.S_addr); 
server_addr.sin_port = htons(PORT);

if (SOCKET_ERROR == connect(c_Socket, (LPSOCKADDR)&server_addr, sizeof(server_addr)))
{

printf("Can Not Connect To Server IP!\n");
system("pause");
exit(1);

}
//输入文件名
char message[MESSAGE_MAX_SIZE + 1];
memset(message, 0, MESSAGE_MAX_SIZE + 1);
printf("Please Input Message To Server: ");
char c;
int i = 0;
c = getchar();
while (c != ‘#‘)
{

message[i] = c;
i++;
c = getchar();

}

char buffer[BUFFER_SIZE];
memset(buffer, 0, BUFFER_SIZE);
strncpy_s(buffer, message, strlen(message)>BUFFER_SIZE ? BUFFER_SIZE : strlen(message));

//向服务器发送消息
if (send(c_Socket, buffer, BUFFER_SIZE, 0) < 0)
{

printf("Send Message Failed\n");
system("pause");
exit(1);

}
closesocket(c_Socket);
//释放winsock库
WSACleanup();
return 0;
}

 

TCP Socket 通讯(客户端与服务端)

标签:

原文地址:http://www.cnblogs.com/lsh123/p/5932021.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!