Makefile文件
socket_ser socket_cl:socket_ser.c socket_cl.c
gcc socket_ser.c -o socket_ser
gcc socket_cl.c -o socket_cl
#.PHONY:clean
clean:
-rm -f socket_ser socket_cl
1、阻塞方式
服务器:
#include <stdio.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <stdlib.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#define MAX_CONN 10
void accept_handle(int connfd, struct sockaddr_in peeraddr);
int main(int argc, const char *argv[])
{
int sockfd, connfd;
struct sockaddr_in seraddr, peeraddr;
socklen_t addrlen;
int mode = 1;
if (argc != 2) {
printf("Usage: %s <port>\n", argv[0]);
return -1;
}
//sockfd = socket(AF_INET, SOCK_STREAM | SOCK_NONBLOCK, 0);//异步
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0) {
printf("socket: %s\n", strerror(errno));
return -1;
}
seraddr.sin_family = AF_INET;
seraddr.sin_port = ntohs(atoi(argv[1]));
seraddr.sin_addr.s_addr = htonl(INADDR_ANY);
if (bind(sockfd, (struct sockaddr *)&seraddr, sizeof(struct sockaddr))) {
printf("bind: %s\n", strerror(errno));
return -1;
}
if (listen(sockfd, MAX_CONN) < 0) {
printf("listen: %s\n", strerror(errno));
return -1;
}
addrlen = sizeof(struct sockaddr);
for (;;) {
connfd = accept(sockfd, (struct sockaddr *)(&peeraddr), &addrlen);
if (connfd < 0) {
printf("accept: %s\n", strerror(errno));
continue;
}
accept_handle(connfd, peeraddr);
}
return 0;
}
void accept_handle(int connfd, struct sockaddr_in peeraddr)
{
char buf[32] = "Hello, welcome to server!";
char buf_h[255];
char recvbuf[255];
int rntCode = 0;
int rntCodel = 0;
printf("Receive request from %s, port %d\n",
inet_ntop(AF_INET, &peeraddr.sin_addr, buf, sizeof(buf)),
ntohs(peeraddr.sin_port));
while(1)
{
bzero(buf_h,sizeof(buf_h));
read(0,buf_h,sizeof(buf_h));// 会发生阻塞
rntCodel = send(connfd, buf_h, sizeof(buf_h), 0);
printf("server 1\n");
if ( rntCodel> 0) {
printf("send info: %s\n", buf_h);
}
memset(recvbuf, 0, sizeof(recvbuf));
rntCode = recv(connfd, recvbuf, sizeof(recvbuf), 0);
//rntCode = read(connfd, recvbuf, sizeof(recvbuf));
if(rntCode > 0)
{
printf("From client: %s\n", recvbuf);
}
}
close(connfd);
return;
}
客户端:
#include <stdio.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <stdlib.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <fcntl.h>
#include <sys/ioctl.h>
int main(int argc, const char *argv[])
{
int sockfd;
struct sockaddr_in seraddr;
char recvbuf[255];
int rntCode = 0;
char buf_h[255];
int rntCodel = 0;
int mode = 1;
if (argc != 3) {
printf("Usage: %s <ip> <port>\n", argv[0]);
return -1;
}
if (inet_pton(AF_INET, argv[1], &seraddr.sin_addr) < 0) {
printf("inet_pton: %s\n", strerror(errno));
return -1;
}
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0) {
printf("socket: %s\n", strerror(errno));
return -1;
}
seraddr.sin_family = AF_INET;
seraddr.sin_port = htons(atoi(argv[2]));
if (connect(sockfd, (struct sockaddr *)(&seraddr),
sizeof(struct sockaddr)) < 0) {
printf("connect: %s\n", strerror(errno));
return -1;
}
while(1)
{
memset(recvbuf, 0, sizeof(recvbuf));
//rntCode = recv(sockfd, recvbuf, sizeof(recvbuf), 0);
rntCode = read(sockfd, recvbuf, sizeof(recvbuf));
if(rntCode > 0)
{
printf("From server: %s\n", recvbuf);
}
bzero(buf_h,sizeof(buf_h));
read(0,buf_h,sizeof(buf_h));
rntCodel = send(sockfd, buf_h, sizeof(buf_h), 0);
if (rntCodel > 0) {
printf("send info: %s\n", buf_h);
}
}
close(sockfd);
return 0;
}
2、非阻塞方式