socket_server.cpp
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <unistd.h>
#define PORT 6666 // 端口
#define MAXLINE 4096 // 字符串长度
#define BACKLOG 10 // 侦听队列长度
void conn_server(int s)
{
while (true)
{
char buff[MAXLINE];
int n;
n = read(s, buff, MAXLINE);
printf("%d read len %d\n",getpid(), n);
if (0 > n)
{
if (errno == ECONNRESET)
{
close(s);
}
else
{
printf("read socket error: %s(errno: %d)", strerror(errno), errno);
}
exit(0);
}
else if (0 == n)
{
close(s);
exit(0);
}
buff[n] = '\0';
printf("recv msg from client: %s\n", buff);
}
}
int main(int argc, char** argv)
{
int listenfd, connfd;
struct sockaddr_in servaddr;
pid_t pid;
if ((listenfd = socket(AF_INET, SOCK_STREAM, 0)) == -1)
{
printf("create socket error: %s(errno: %d)\n", strerror(errno), errno);
exit(0);
}
unsigned value = 1;
setsockopt(listenfd, SOL_SOCKET, SO_REUSEADDR, &value, sizeof(value));
bzero(&servaddr, sizeof(servaddr));
servaddr.sin_family = AF_INET; // 协议族
servaddr.sin_addr.s_addr = htonl(INADDR_ANY ); // 本地地址
servaddr.sin_port = htons(PORT); // 服务器端口
if (bind(listenfd, (struct sockaddr*) &servaddr, sizeof(servaddr)) == -1)
{
printf("bind socket error: %s(errno: %d)\n", strerror(errno), errno);
exit(0);
}
if (listen(listenfd, BACKLOG) == -1)
{
printf("listen socket error: %s(errno: %d)\n", strerror(errno), errno);
exit(0);
}
printf("======waiting for client's request======\n");
while (true)
{
if ((connfd = accept(listenfd, (struct sockaddr*) NULL, NULL)) == -1)
{
printf("accept socket error: %s(errno: %d)", strerror(errno),
errno);
continue;
}
pid = fork();
if(0 == pid)
{
close(listenfd);
conn_server(connfd);
}
close(connfd);
}
close(listenfd);
}
socket_client.cpp
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#define MAXLINE 4096
#define PORT 6666
int main(int argc, char** argv)
{
int sockfd, n;
char sendline[MAXLINE];
struct sockaddr_in servaddr;
if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
{
printf("create socket error: %s(errno: %d)\n", strerror(errno), errno);
exit(0);
}
bzero(&servaddr, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_port = htons(PORT);
if (2 == argc)
{
if (inet_pton(AF_INET, argv[1], &servaddr.sin_addr) <= 0)
{
printf("inet_pton error for %s\n", argv[1]);
exit(0);
}
}
else
{
servaddr.sin_addr.s_addr = htonl(INADDR_ANY );
}
if (connect(sockfd, (struct sockaddr*) &servaddr, sizeof(servaddr)) < 0)
{
printf("connect error: %s(errno: %d)\n", strerror(errno), errno);
exit(0);
}
while (1)
{
printf("send msg to server: \n");
scanf("%s",sendline);
if (send(sockfd, sendline, strlen(sendline), 0) < 0)
{
printf("send msg error: %s(errno: %d)\n", strerror(errno), errno);
exit(0);
}
}
close(sockfd);
exit(0);
}
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <unistd.h>
#define PORT 6666 // 端口
#define MAXLINE 4096 // 字符串长度
#define BACKLOG 10 // 侦听队列长度
void conn_server(int s)
{
while (true)
{
char buff[MAXLINE];
int n;
n = read(s, buff, MAXLINE);
printf("%d read len %d\n",getpid(), n);
if (0 > n)
{
if (errno == ECONNRESET)
{
close(s);
}
else
{
printf("read socket error: %s(errno: %d)", strerror(errno), errno);
}
exit(0);
}
else if (0 == n)
{
close(s);
exit(0);
}
buff[n] = '\0';
printf("recv msg from client: %s\n", buff);
}
}
int main(int argc, char** argv)
{
int listenfd, connfd;
struct sockaddr_in servaddr;
pid_t pid;
if ((listenfd = socket(AF_INET, SOCK_STREAM, 0)) == -1)
{
printf("create socket error: %s(errno: %d)\n", strerror(errno), errno);
exit(0);
}
unsigned value = 1;
setsockopt(listenfd, SOL_SOCKET, SO_REUSEADDR, &value, sizeof(value));
bzero(&servaddr, sizeof(servaddr));
servaddr.sin_family = AF_INET; // 协议族
servaddr.sin_addr.s_addr = htonl(INADDR_ANY ); // 本地地址
servaddr.sin_port = htons(PORT); // 服务器端口
if (bind(listenfd, (struct sockaddr*) &servaddr, sizeof(servaddr)) == -1)
{
printf("bind socket error: %s(errno: %d)\n", strerror(errno), errno);
exit(0);
}
if (listen(listenfd, BACKLOG) == -1)
{
printf("listen socket error: %s(errno: %d)\n", strerror(errno), errno);
exit(0);
}
printf("======waiting for client's request======\n");
while (true)
{
if ((connfd = accept(listenfd, (struct sockaddr*) NULL, NULL)) == -1)
{
printf("accept socket error: %s(errno: %d)", strerror(errno),
errno);
continue;
}
pid = fork();
if(0 == pid)
{
close(listenfd);
conn_server(connfd);
}
close(connfd);
}
close(listenfd);
}
socket_client.cpp
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#define MAXLINE 4096
#define PORT 6666
int main(int argc, char** argv)
{
int sockfd, n;
char sendline[MAXLINE];
struct sockaddr_in servaddr;
if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
{
printf("create socket error: %s(errno: %d)\n", strerror(errno), errno);
exit(0);
}
bzero(&servaddr, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_port = htons(PORT);
if (2 == argc)
{
if (inet_pton(AF_INET, argv[1], &servaddr.sin_addr) <= 0)
{
printf("inet_pton error for %s\n", argv[1]);
exit(0);
}
}
else
{
servaddr.sin_addr.s_addr = htonl(INADDR_ANY );
}
if (connect(sockfd, (struct sockaddr*) &servaddr, sizeof(servaddr)) < 0)
{
printf("connect error: %s(errno: %d)\n", strerror(errno), errno);
exit(0);
}
while (1)
{
printf("send msg to server: \n");
scanf("%s",sendline);
if (send(sockfd, sendline, strlen(sendline), 0) < 0)
{
printf("send msg error: %s(errno: %d)\n", strerror(errno), errno);
exit(0);
}
}
close(sockfd);
exit(0);
}