最终实现如下:
项目解决方案管理器如下:(使用的软件是 VS2015)
服务端代码如下:
头文件:tcpSocket.h
#ifndef _TCPSOCKET_H_
#define _TCPSOCKET_H_
#include<WinSock2.h> //windows平台的网络库头文件
#pragma comment(lib,"ws2_32.lib") //库文件
#include<stdbool.h>
#include<stdio.h>
#define PORT 8888 //[0,65536) 0~1024 是保留窗口
#define err(errMsg) printf("[line:%d]%s failed code %d \n",__LINE__,errMsg, WSAGetLastError())
//打开网络库
bool init_Socket();
//关闭网络库
bool close_Socket();
//创建服务器
SOCKET createServerSocket();
//创建客户端socket
SOCKET createClientSocket(const char *ip);
#endif // !_TCPSOCKET_H_
源文件:server.c 、tcpSocket.c
#include "stdlib.h"
#include"../tcpSocket/tcpSocket.h"
int main()
{
init_Socket();
//err("自己");
SOCKET serfd = createServerSocket();
printf("wait client connect...\n");
//如果有客户端请求连接
SOCKET clifd = accept(serfd,NULL,NULL);
if (INVALID_SOCKET == clifd)
{
err("accept");
}
//可以和客户端进行通信了
char recvbuf[BUFSIZ] = { 0 };
char sendbuf[BUFSIZ] = { 0 };
while (true)
{
//recv从指定的socket接受消息
if (0 < recv(clifd, recvbuf, BUFSIZ, 0))
{
printf("recv:%s\n", recvbuf);
}
//发送消息
printf("send>");
memset(sendbuf, 0, sizeof(sendbuf));
gets_s(sendbuf, BUFSIZ);
if (SOCKET_ERROR == send(clifd, sendbuf, strlen(sendbuf), 0))
{
err("send");
}
}
closesocket(clifd);
closesocket(serfd);
close_Socket();
printf("server-----end-----\n");
getchar();
/*system("pause");*/
return 0;
}
源文件:tcpSocket.c
#include"tcpSocket.h"
bool init_Socket()
{
WSADATA wasdata;
if (0 != WSAStartup(MAKEWORD(2, 2), &wasdata))
{
printf("WSAStartup failed code %d", WSAGetLastError());
return false;
}
return true;
}
bool close_Socket()
{
if (0 != WSACleanup())
{
printf("WSACleanup failed code %d", WSAGetLastError());
return false;
}
return true;
}
SOCKET createServerSocket()
{
//1.创建新的Socket -买新手机
SOCKET fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (fd == INVALID_SOCKET)
{
err("socket");
return INVALID_SOCKET;
}
//2.给socket绑定ip地址和端口号 -买卡才能打电话
struct sockaddr_in addr;
addr.sin_family = AF_INET; //和创建socket时必须一样
addr.sin_port = htons(PORT); //大端存储 小端存储
addr.sin_addr.S_un.S_addr = ADDR_ANY;//inet_addr("127.0.0.1");
if (SOCKET_ERROR == bind(fd, &addr, sizeof(addr)))
{
err("bind");
return false;
}
//3.监听电话
listen(fd, 10);
return fd;
}
SOCKET createClientSocket(const char *ip)
{
//1.创建新的Socket -buy new phone
SOCKET fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (fd == INVALID_SOCKET)
{
err("socket");
return INVALID_SOCKET;
}
//2.与服务器建立连接
struct sockaddr_in addr;
addr.sin_family = AF_INET; //和创建socket时必须一样
addr.sin_port = htons(PORT); //大端存储 小端存储
addr.sin_addr.S_un.S_addr = inet_addr(ip); //"127.0.0.1"
if (INVALID_SOCKET == connect(fd, (struct sockaddr*)&addr, sizeof(addr)))
{
err("connect");
return false;
}
return fd;
}
客户端代码如下:
头文件:tcpSocket.h
#ifndef _TCPSOCKET_H_
#define _TCPSOCKET_H_
#include<WinSock2.h> //windows平台的网络库头文件
#pragma comment(lib,"ws2_32.lib") //库文件
#include<stdbool.h>
#include<stdio.h>
#define PORT 8888 //[0,65536) 0~1024 是保留窗口
#define err(errMsg) printf("[line:%d]%s failed code %d \n",__LINE__,errMsg, WSAGetLastError())
//打开网络库
bool init_Socket();
//关闭网络库
bool close_Socket();
//创建服务器
SOCKET createServerSocket();
//创建客户端socket
SOCKET createClientSocket(const char *ip);
#endif // !_TCPSOCKET_H_
源文件:client.c
#include"../tcpSocket/tcpSocket.h"
#include<stdlib.h>
int main()
{
init_Socket();
SOCKET fd = createClientSocket("127.0.0.1");
char recvbuf[BUFSIZ] = { 0 }; //"你好我是客户端QAQ";
char sendbuf[BUFSIZ] = { 0 };
//先说一句话,打个招呼
//if (SOCKET_ERROR == send(fd,buf,strlen(buf),0))
//{
// err("send");
//}
while (true)
{
//发送消息
printf("send>");
gets_s(sendbuf, BUFSIZ);
if (SOCKET_ERROR == send(fd, sendbuf, strlen(sendbuf), 0))
{
err("send");
}
//recv从指定的socket接受消息
if (0 < recv(fd, recvbuf, BUFSIZ, 0))
{
printf("recv:%s\n", recvbuf);
memset(sendbuf, 0, sizeof(sendbuf));
}
}
closesocket(fd);
close_Socket();
printf("client-----end-----\n");
getchar();
return 0;
}
源文件:tcpSocket.c
#include"tcpSocket.h"
bool init_Socket()
{
WSADATA wasdata;
if (0 != WSAStartup(MAKEWORD(2, 2), &wasdata))
{
printf("WSAStartup failed code %d", WSAGetLastError());
return false;
}
return true;
}
bool close_Socket()
{
if (0 != WSACleanup())
{
printf("WSACleanup failed code %d", WSAGetLastError());
return false;
}
return true;
}
SOCKET createServerSocket()
{
//1.创建新的Socket -买新手机
SOCKET fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (fd == INVALID_SOCKET)
{
err("socket");
return INVALID_SOCKET;
}
//2.给socket绑定ip地址和端口号 -买卡才能打电话
struct sockaddr_in addr;
addr.sin_family = AF_INET; //和创建socket时必须一样
addr.sin_port = htons(PORT); //大端存储 小端存储
addr.sin_addr.S_un.S_addr = ADDR_ANY;//inet_addr("127.0.0.1");
if (SOCKET_ERROR == bind(fd, &addr, sizeof(addr)))
{
err("bind");
return false;
}
//3.监听电话
listen(fd, 10);
return fd;
}
SOCKET createClientSocket(const char *ip)
{
//1.创建新的Socket -buy new phone
SOCKET fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (fd == INVALID_SOCKET)
{
err("socket");
return INVALID_SOCKET;
}
//2.与服务器建立连接
struct sockaddr_in addr;
addr.sin_family = AF_INET; //和创建socket时必须一样
addr.sin_port = htons(PORT); //大端存储 小端存储
addr.sin_addr.S_un.S_addr = inet_addr(ip); //"127.0.0.1"
if (INVALID_SOCKET == connect(fd, (struct sockaddr*)&addr, sizeof(addr)))
{
err("connect");
return false;
}
return fd;
}
注意:
服务端与客户端的 tcpSocket.h 、tcpSocket.c 这两个文件是一样的。