net socket
TestSocket.h
//
// Created by ozcomcn on 10/22/19.
//
#ifndef SOCKET_S_TESTSOCKET_H
#define SOCKET_S_TESTSOCKET_H
#include <iostream>
template<typename __Type>
class ToUpper
{
public:
inline __Type operator()(__Type &__t)
{
return toupper(__t);
}
};
#endif //SOCKET_S_TESTSOCKET_H
server
#include <iostream>
#include <sys/types.h>
#include <unistd.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <sys/un.h>
#include <thread>
#include <cstring>
#include <algorithm>
#include <functional>
#include "TestSocket.h"
#define PROTOCOL 0
#define PORT 9999
#define DOMAIN AF_INET
#define TYPE SOCK_STREAM
#define S_IP "127.0.0.1"
inline void server()
{
int listen_fd, c_fd;
struct sockaddr_in sockAddr{}, acceptAddr{};
socklen_t c_addr_len;
char buf[BUFSIZ];
memset(buf, 0, BUFSIZ);
memset(&sockAddr, 0, sizeof(sockAddr));
memset(&acceptAddr, 0, sizeof(acceptAddr));
listen_fd = socket(DOMAIN, TYPE, PROTOCOL);
if (listen_fd < 0)
{
perror("create socket failure !");
exit(errno);
}
sockAddr.sin_family = AF_INET;
sockAddr.sin_port = htons(PORT);
sockAddr.sin_addr.s_addr = htonl(INADDR_ANY);
if (bind(listen_fd, (struct sockaddr *) &sockAddr, sizeof(sockAddr)) != 0)
{
perror("bind socket failure !");
exit(errno);
}
if (listen(listen_fd, 200) != 0)
{
perror("listen socket failure !");
exit(errno);
}
c_fd = accept(listen_fd, (struct sockaddr *) &acceptAddr, &c_addr_len);
if (c_fd < 0)
sockAddr.sin_addr.s_addr = htonl(INADDR_ANY);
{
perror("accept socket failure !");
exit(errno);
}
while (true)
{
size_t len = read(c_fd, buf, sizeof(buf));
if (len == 0) continue;
if (strstr(buf, "exit"))
{
break;
}
printf("%s\n", buf);
std::transform(buf, buf + len, buf, ToUpper<char>());
write(c_fd, buf, len);
memset(buf, 0, len);
}
close(c_fd);
close(listen_fd);
printf("exit!!!");
}
client
#include <iostream>
#include <sys/types.h>
#include <unistd.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <thread>
#include <cstring>
#include <sys/un.h>
#define PROTOCOL 0
#define PORT 9999
#define DOMAIN AF_INET
#define TYPE SOCK_STREAM
#define S_IP "127.0.0.1"
inline void client()
{
int c_fd;
struct sockaddr_in sAddr{};
socklen_t c_addr_len;
char buf[BUFSIZ];
c_fd = socket(DOMAIN, TYPE, PROTOCOL);
if (c_fd < 0)
{
perror("create socket failure !");
exit(errno);
}
sAddr.sin_family = AF_INET;
sAddr.sin_port = htons(PORT);
sAddr.sin_addr.s_addr = inet_addr(S_IP);
if (connect(c_fd, (sockaddr *) &sAddr, sizeof(sAddr)) != 0)
{
perror("connect socket failure !");
}
while (true)
{
scanf("%s", buf);
printf("\n");
int len;
len = send(c_fd, buf, strlen(buf), 0);
if (strstr(buf, "exit"))
{
break;
}
memset(buf, 0, len);
len = recv(c_fd, buf, sizeof(buf), 0);
printf("%s\n", buf);
memset(buf, 0, len);
}
close(c_fd);
printf("exit!!!");
}
local socket
server
#include <iostream>
#include <sys/types.h>
#include <unistd.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <thread>
#include <cstring>
#include <sys/un.h>
#define LOCAL_SOCK_PATH "/tmp/test_local_sock"
#define PROTOCOL 0
#define TYPE SOCK_STREAM
void local_server()
{
if (access(LOCAL_SOCK_PATH, F_OK) == 0)
{
unlink(LOCAL_SOCK_PATH);
}
int listen_fd, c_fd;
struct sockaddr_un sockAddr{}, acceptAddr{};
socklen_t c_addr_len;
char buf[BUFSIZ];
memset(&sockAddr, 0, sizeof(sockAddr));
memset(&acceptAddr, 0, sizeof(acceptAddr));
memset(buf, 0, BUFSIZ);
listen_fd = socket(AF_UNIX, TYPE, PROTOCOL);
if (listen_fd < 0)
{
perror("create socket failure !");
exit(errno);
}
sockAddr.sun_family = AF_UNIX;
strcpy(sockAddr.sun_path, LOCAL_SOCK_PATH);
if (bind(listen_fd, (struct sockaddr *) &sockAddr, sizeof(sockAddr)) != 0)
{
perror("bind socket failure !");
exit(errno);
}
if (listen(listen_fd, 200) != 0)
{
perror("listen socket failure !");
exit(errno);
}
c_fd = accept(listen_fd, (struct sockaddr *) &acceptAddr, &c_addr_len);
if (c_fd < 0)
{
perror("accept socket failure !");
exit(errno);
}
while (true)
{
size_t len = read(c_fd, buf, sizeof(buf));
if (len == 0) continue;
if (strstr(buf, "exit"))
{
break;
}
printf("%s\n", buf);
std::transform(buf, buf + len, buf, ToUpper<char>());
write(c_fd, buf, len);
memset(buf, 0, len);
}
close(c_fd);
close(listen_fd);
unlink(LOCAL_SOCK_PATH);
printf("exit!!!");
}
client
#include <iostream>
#include <sys/types.h>
#include <unistd.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <thread>
#include <cstring>
#include <sys/un.h>
#define LOCAL_SOCK_PATH "/tmp/test_local_sock"
#define PROTOCOL 0
#define TYPE SOCK_STREAM
void local_client()
{
int c_fd;
struct sockaddr_un sAddr{};
socklen_t c_addr_len;
char buf[BUFSIZ];
c_fd = socket(AF_UNIX, TYPE, PROTOCOL);
if (c_fd < 0)
{
perror("create socket failure !");
exit(errno);
}
sAddr.sun_family = AF_UNIX;
strcpy(sAddr.sun_path, LOCAL_SOCK_PATH);
if (connect(c_fd, (sockaddr *) &sAddr, sizeof(sAddr)) != 0)
{
perror("connect socket failure !");
}
while (true)
{
scanf("%s", buf);
printf("\n");
int len;
len = send(c_fd, buf, strlen(buf), 0);
if (strstr(buf, "exit"))
{
break;
}
memset(buf, 0, len);
len = recv(c_fd, buf, sizeof(buf), 0);
printf("%s\n", buf);
memset(buf, 0, len);
}
close(c_fd);
printf("exit!!!");
}
c++大小写转换的9种实现方式
#include <iostream>
#include <cstring>
void test_toUpper()
{
char content[] = "qqrrtetwertw";
size_t len = strlen(content);
/*1*/
// std::transform(content, content + len, content, toupper);
/*2*/
// std::function<int(int)> toUp = toupper;
//
// std::transform(content, content + len, content, toUp);
/*3*/
// std::transform(content, content + len, content, (int (*)(int)) toupper);
/*4*/
// std::transform(content, content + len, content, ::toupper);
/*5*/
// std::transform(content, content + strlen(content), content, [](auto &ch) -> int
// {
// return toupper(ch);
// });
/*6*/
// std::for_each(content, content + strlen(content), [](auto &ch)
// {
// ch = toupper(ch);
// });
/*7*/
// for (size_t i = 0; i < len; i++)
// {
// content[i] = static_cast<char>(toupper(content[i]));
// }
/*8*/
// auto toUpper = std::bind(ToUpper<char>(), std::placeholders::_1);
//
// for (size_t i = 0; i < len; i++)
// {
// content[i] = static_cast<char>(toUpper(content[i]));
// }
/*9*/
std::transform(content, content + len, content, ToUpper<char>());
printf("%s\n", content);
}
网友评论