tcpsocket.h
#pragma once
using namespace std;
#include <iostream>
#ifndef _TCPSOCKET_H_
#define _TCPSOCKET_H_
#endif // !_TCPSOCKET_H_
#define _WINSOCK_DEPRECATED_NOWARNINGS
#include <stdbool.h>
#define PORT 9000
#define err(errMsg,fd) printf("[line::%d]%s failed code %d\r\n",__LINE__,errMsg,fd)
// TODO: 在此处引用程序需要的其他标头。
#include <stdio.h>//标准输入输出头文件
#include <string.h>//关于字符数组的函数定义的头文件
#include <stdlib.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <sys/socket.h>
int createServeSocket();
int createClientSocket(const char* ip);
char* gets_s(char* str, int num);
tcpsocket自定义初始化
#include "tcpSocket.h"
int createServeSocket()
{
//1,创建空的Socket
//parm1:af 地址协议族 ipv4 ipv6
//parm2:type 传输协议类型数据报流式套接字
//parm3:protoc1 使用具体的某个传输协议
int fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (-1==fd) {
err("server-socket",fd);
return fd;
}
//2,给socket绑定ip地址和端口号
struct sockaddr_in addr;
addr.sin_family = AF_INET;
//htons的功能:将一个无符号短整型数值转换为网络字节序,即大端模式(big - endian)
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, (const sockaddr*)&addr, sizeof(addr))) {
err("server-bind");
return false;
}
//3.监听客户端
listen(fd, 10);
return fd;
}
SOCKET createClientSocket(const char* ip)
{
//1,创建空的Socket
//parm1:af 地址协议族 ipv4 ipv6
//parm2:type 传输协议类型数据报流式套接字
//parm3:protoc1 使用具体的某个传输协议
SOCKET fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (fd == INVALID_SOCKET) {
err("client-socket");
return INVALID_SOCKET;
}
//2,和服务器建立连接
struct sockaddr_in addr;
addr.sin_family = AF_INET;
//htons的功能:将一个无符号短整型数值转换为网络字节序,即大端模式(big - endian)
addr.sin_port = htons(PORT);
addr.sin_addr.S_un.S_addr = inet_addr(ip); //ADDR_ANY;
while (true) {
if (SOCKET_ERROR == connect(fd, (const sockaddr*)&addr, sizeof(addr))) {
err("client-connect");
}
else {
printf("client-connect success");
break;
}
}
return fd;
}
char* gets_s(char* buffer, int num) //gets_s函数gets_s手工实现
{
if (fgets(buffer, num, stdin) != 0)
{
size_t len = strlen(buffer);
if (len > 0 && buffer[len - 1] == '\n')
buffer[len - 1] = '\0';
return buffer;
}
return NULL;
}
客户端
client.cpp
#include "Client.h"
#include "tcpSocket.h"
#include <iostream>
#include<time.h>
using namespace std;
int main()
{
init_Socket();
SOCKET clientfd = createClientSocket("192.168.244.1");
char recvbuf[BUFSIZ] = "+++++66++++++";
char sendbuf[BUFSIZ] = { 0 };
while (true) {
printf("client-send>>");
gets_s(sendbuf, BUFSIZ);
if (SOCKET_ERROR == send(clientfd, sendbuf, strlen(sendbuf), 0)) {
err("client-send");
}
if (0 < recv(clientfd, recvbuf,BUFSIZ, 0)) {
printf("client-recv:%s\r\n",recvbuf);
memset(recvbuf,0,BUFSIZ);
}
}
closesocket(clientfd);
close_Socket();
return 0;
}
服务器service.cpp
#include "Server.h"
#include "tcpSocket.h"
#include <iostream>
using namespace std;
int main()
{
cout << "hello world" << endl;
init_Socket();
SOCKET serverfd=createServeSocket();
SOCKET clientfd=accept(serverfd, NULL, NULL);
if (INVALID_SOCKET == clientfd) {
err("server accept");
}
char recvbuf[BUFSIZ] = { 0 };
char sendbuf[BUFSIZ] = { 0 };
while (true) {
if (0 < recv(clientfd, recvbuf, BUFSIZ, 0)) {
printf(" server.recv %s\r\n", recvbuf);
memset(recvbuf, 0, BUFSIZ);
}
printf("server-send>>");
gets_s(sendbuf,BUFSIZ);
if (INVALID_SOCKET == send(clientfd, sendbuf, strlen(sendbuf), 0)) {
err("server-send");
return false;
}
}
closesocket(serverfd);
closesocket(clientfd);
close_Socket();
return 0;
}