/* sockclnt.c*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h> /*for struct sockaddr_in*/
#include <signal.h>
#define SVR_IP "12.12.12.221"
#define SVR_PORT 1234
#define BUFSIZE 255
int sockfd = 0;
void mysigint(int sig)
{
printf("close fd = %d
",sockfd);
close(sockfd);
}
int main()
{
signal(5,mysigint);
int ret = 0;
struct sockaddr_in svr_addr;
memset(&svr_addr, 0, sizeof(struct sockaddr_in));
char * msg = "'path:/home/dongshen/media_file/b_1.ts'";
// create client socket fd
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd == -1) {
perror("socket failed");
exit(1);
}
// connet to server
svr_addr.sin_family = AF_INET;
svr_addr.sin_port = htons(SVR_PORT);
svr_addr.sin_addr.s_addr = inet_addr(SVR_IP);
printf("before connect
");
ret = connect(sockfd, (struct sockaddr *)&svr_addr, sizeof(struct sockaddr_in));
if (ret == -1) {
perror("connect failediiiii");
exit(1);
}
printf("after connect
");
int i = 0;
for (i = 0; i < 500; i++)
{
char * msg = "hello server....";
printf("client send msg: %s
", msg);
if((send(sockfd, msg, strlen(msg), 0))<0)
{
perror("send()");
}
char recv_buf[BUFSIZE] = {0};
int recv_len = recv(sockfd, recv_buf, sizeof(recv_buf), 0);
printf("client recv msg: %s%d
", recv_buf, recv_len);
sleep(100);
}
close(sockfd);
}
服务端
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h> /* for struct sockaddr_in*/
#include <sys/errno.h>
#include <signal.h>
#define SVR_IP "12.12.12.221"
#define SVR_PORT 1234
#define BACKLOG 10
#define MAX_BUF_LEN 1024
int connfd = 0;
void mysigint(int sig)
{
printf("sigint i will close connfd = %d
",connfd);
close(connfd);
}
void chld_handle(int sig)
{
pid_t pid;
int stat;
while ((pid = waitpid(-1, &stat, WNOHANG)) > 0)
{
printf("child %d terminated
", pid);
}
return;
}
int main()
{
struct sigaction act;
act.sa_handler = chld_handle;
sigemptyset(&act.sa_mask);
act.sa_flags = SA_RESTART;
sigaction(SIGCHLD, &act, 0);
int listenfd = 0;
int ret = 0;
struct sockaddr_in svr_addr, cli_addr;
memset(&svr_addr, 0, sizeof(struct sockaddr_in));
memset(&cli_addr, 0, sizeof(struct sockaddr_in));
// create listen socket fd
listenfd = socket(AF_INET, SOCK_STREAM, 0);
if (listenfd == -1) {
perror("socket failed");
exit(1);
}
// bind svr ip and port
svr_addr.sin_family = AF_INET;
svr_addr.sin_port = htons(SVR_PORT);
svr_addr.sin_addr.s_addr = inet_addr(SVR_IP);
ret = bind(listenfd, (struct sockaddr*)&svr_addr, sizeof(struct sockaddr));
if (ret == -1) {
perror("binad failed");
exit(1);
}
// listen client
ret = listen(listenfd, BACKLOG);
if (ret == -1) {
perror("listen failed");
exit(1);
}
printf("server is on...
");
while(1)
{
int sin_size = sizeof(struct sockaddr_in);
if((connfd = accept(listenfd, (struct sockaddr *)&cli_addr, &sin_size)) < 0)
{
perror("accept failed");
if (errno == EINTR)
{
perror("child process cause it
");
continue;
}
else
{
break;
}
}
pid_t child_pid = fork();
if (0 == child_pid) // this is child process
{
close(listenfd);
printf("one socket(%d) client come
", connfd);
signal(SIGINT,mysigint);
char recv_buf[MAX_BUF_LEN] = {0};
int recv_len = 0;
while((recv_len = recv(connfd, recv_buf, sizeof(recv_buf), 0)) > 0)
{
printf("svr %d recv %s recv_len %d
", connfd, recv_buf, recv_len);
ret = send(connfd, recv_buf, strlen(recv_buf), 0);
if (ret < 0)
{
perror("send failed");
exit(1);
}
memset(recv_buf, 0, sizeof(recv_buf));
}
if (recv_len < 0)
{
perror("recv failed");
exit(1);
}
printf("one socket(%d) client go
", connfd);
close(connfd);
exit(0);
}
close(connfd);
}
close(listenfd);
return 0;
}