네트워크 프로그래밍 코드 실례
#include
#include
#include
#include
#include
#include
#include
#include
#define PORT 8888
int main()
{
int sockfd, ret, fd;
char buf[100] = {0};
struct sockaddr_in server_addr;
struct sockaddr_in client_addr;
printf("Start Server!
");
sockfd = socket(PF_INET, SOCK_STREAM, 0);
if(-1 == sockfd)
{
perror("socket");
exit(1);
}
memset(&server_addr, 0 ,sizeof(server_addr));
server_addr.sin_family = PF_INET;
server_addr.sin_port = PORT;
server_addr.sin_addr.s_addr = inet_addr("127.0.0.1");
ret = bind(sockfd, (struct sockaddr *)&server_addr, sizeof(server_addr));
if(-1 == ret)
{
perror("bind");
exit(1);
}
printf("Listening...
");
ret = listen(sockfd,5);
if(-1 == ret)
{
perror("listen");
exit(1);
}
int length = sizeof(client_addr);
fd = accept(sockfd, (struct sockaddr *)&client_addr, &length);
if(-1 == fd)
{
perror("accept");
exit(1);
}
while(1)
{
ret = recv(fd,buf,sizeof(buf),0);
if(-1 == ret)
{
perror("recv");
exit(1);
}
if(!strcmp(buf,"bye"))
{
break;
}
printf("Recv From Client : %s
",buf);
memset(buf, 0, sizeof(buf));
}
close(sockfd);
close(fd);
return 0;
}
#include
#include
#include
#include
#include
#include
#include
#define PORT 8888
int main()
{
int sockfd, ret;
struct sockaddr_in server_addr;
char buf[100] = {0};
sockfd = socket(PF_INET, SOCK_STREAM, 0);
if(-1 == sockfd)
{
perror("socket");
exit(1);
}
memset(&server_addr, 0 ,sizeof(server_addr));
server_addr.sin_family = PF_INET;
server_addr.sin_port = PORT;
server_addr.sin_addr.s_addr = inet_addr("127.0.0.1");
printf("Start Connecting...
");
ret = connect(sockfd, (struct sockaddr *)&server_addr, sizeof(server_addr));
if(-1 == ret)
{
perror("connect");
exit(1);
}
while(1)
{
scanf("%s",buf);
ret = send(sockfd, buf, strlen(buf), 0);
if(-1 == ret)
{
perror("send");
exit(1);
}
if(!strcmp(buf,"bye"))
{
break;
}
memset(buf, 0, sizeof(buf));
}
close(sockfd);
return 0;
}
서버에 메시지를 보낼 클라이언트를 만들려면 다음과 같이 하십시오.
#include
#include
#include
#include
#include
#include
#include
#include
#define PORT 8888
void *MyReceive(void *arg)
{
char buf[100] = {0};
int ret;
pthread_detach(pthread_self());
while(1)
{
ret = recv(*(int *)arg,buf,sizeof(buf),0);
if(-1 == ret)
{
perror("recv");
exit(1);
}
if(!strcmp(buf,"bye"))
{
close(*(int *)arg);
break;
}
printf("Recv From Client %d : %s
",*(int *)arg,buf);
memset(buf,0,sizeof(buf));
}
}
int main()
{
int fd[100] = {0};
int length , i = 0;
int sockfd, ret;
char buf[100] = {0};
struct sockaddr_in server_addr;
struct sockaddr_in client_addr;
pthread_t tid[100] = {0};
printf("Start Server!
");
sockfd = socket(PF_INET, SOCK_STREAM, 0);
if(-1 == sockfd)
{
perror("socket");
exit(1);
}
memset(&server_addr, 0 ,sizeof(server_addr));
server_addr.sin_family = PF_INET;
server_addr.sin_port = PORT;
server_addr.sin_addr.s_addr = inet_addr("127.0.0.1");
ret = bind(sockfd, (struct sockaddr *)&server_addr, sizeof(server_addr));
if(-1 == ret)
{
perror("bind");
exit(1);
}
printf("Listening...
");
ret = listen(sockfd,5);
if(-1 == ret)
{
perror("listen");
exit(1);
}
while(1)
{
length = sizeof(client_addr);
fd[i] = accept(sockfd, (struct sockaddr *)&client_addr, &length);
if(-1 == fd[i])
{
perror("accept");
exit(1);
}
printf("Accept %d, Port %d
",fd[i],client_addr.sin_port);
ret = pthread_create(&tid[i],NULL,MyReceive,(void *)&fd[i]);
if(0 != ret)
{
perror("pthread_create");
exit(1);
}
i++;
}
close(sockfd);
close(fd);
return 0;
}
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define PORT 8888
struct info
{
char buf[100];
int Tofd;
};
void *MyReceive(void *arg)
{
struct info Recvbuf = {0};
int ret;
pthread_detach(pthread_self());
while(1)
{
ret = recv(*(int *)arg,&Recvbuf,sizeof(Recvbuf),0);
if(-1 == ret)
{
perror("recv");
exit(1);
}
/* if(!strcmp(buf,"bye"))
{
close(*(int *)arg);
break;
}
printf("Recv From Client %d : %s
",*(int *)arg,buf);*/
ret = send(Recvbuf.Tofd,&Recvbuf,sizeof(Recvbuf),0);
if(-1 == ret)
{
perror("send");
exit(1);
}
memset(&Recvbuf,0,sizeof(Recvbuf));
}
}
int main()
{
int fd[100] = {0};
int length , i = 0;
int sockfd, ret;
char buf[100] = {0};
struct sockaddr_in server_addr;
struct sockaddr_in client_addr;
pthread_t tid[100] = {0};
printf("Start Server!
");
sockfd = socket(PF_INET, SOCK_STREAM, 0);
if(-1 == sockfd)
{
perror("socket");
exit(1);
}
memset(&server_addr, 0 ,sizeof(server_addr));
server_addr.sin_family = PF_INET;
server_addr.sin_port = PORT;
server_addr.sin_addr.s_addr = inet_addr("127.0.0.1");
ret = bind(sockfd, (struct sockaddr *)&server_addr, sizeof(server_addr));
if(-1 == ret)
{
perror("bind");
exit(1);
}
printf("Listening...
");
ret = listen(sockfd,5);
if(-1 == ret)
{
perror("listen");
exit(1);
}
while(1)
{
length = sizeof(client_addr);
fd[i] = accept(sockfd, (struct sockaddr *)&client_addr, &length);
if(-1 == fd[i])
{
perror("accept");
exit(1);
}
printf("Accept %d, Port %d
",fd[i],client_addr.sin_port);
ret = pthread_create(&tid[i],NULL,MyReceive,(void *)&fd[i]);
if(0 != ret)
{
perror("pthread_create");
exit(1);
}
i++;
}
close(sockfd);
// close(fd);
return 0;
}
#include
#include
#include
#include
#include
#include
#include
#include
#define PORT 8888
struct info
{
char buf[100];
int Tofd;
};
pthread_t tid[2] = {0};
void *Send(void *arg)
{
struct info Sendbuf = {0};
int ret;
while(1)
{
scanf("%s%d",Sendbuf.buf,&Sendbuf.Tofd);
ret = send(*(int *)arg,&Sendbuf, sizeof(Sendbuf), 0);
if(-1 == ret)
{
perror("send");
exit(1);
}
/*if(!strcmp(buf,"bye"))
{
break;
}*/
memset(&Sendbuf, 0, sizeof(Sendbuf));
}
}
void *Recv(void *arg)
{
struct info Recvbuf = {0};
int ret;
while(1)
{
ret = recv(*(int *)arg,&Recvbuf,sizeof(Recvbuf),0);
if(-1 == ret)
{
perror("recv");
exit(1);
}
printf("\t\t\t%s
",Recvbuf.buf);
memset(&Recvbuf,0,sizeof(Recvbuf));
}
}
int main()
{
int sockfd, ret;
struct sockaddr_in server_addr;
// char buf[100] = {0};
sockfd = socket(PF_INET, SOCK_STREAM, 0);
if(-1 == sockfd)
{
perror("socket");
exit(1);
}
memset(&server_addr, 0 ,sizeof(server_addr));
server_addr.sin_family = PF_INET;
server_addr.sin_port = PORT;
server_addr.sin_addr.s_addr = inet_addr("127.0.0.1");
printf("Start Connecting...
");
ret = connect(sockfd, (struct sockaddr *)&server_addr, sizeof(server_addr));
if(-1 == ret)
{
perror("connect");
exit(1);
}
ret = pthread_create(&tid[0],NULL,Send,(void *)&sockfd);
if(0 != ret)
{
perror("pthread_create1");
exit(1);
}
ret = pthread_create(&tid[1],NULL,Recv,(void *)&sockfd);
if(0 != ret)
{
perror("pthread_create2");
exit(1);
}
pthread_join(tid[0],NULL);
pthread_join(tid[1],NULL);
close(sockfd);
return 0;
}
다중 스레드로 다음과 같은 기능을 수행할 수 있습니다.
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define PORT 8888
struct info
{
int ToFd;
char buf[100];
};
void *MyReceive(void *arg)
{
struct info RecvBuf = {0};
int ret;
pthread_detach(pthread_self());
while (1)
{
ret = recv(*(int *)arg, &RecvBuf, sizeof(RecvBuf), 0);
if (-1 == ret)
{
perror("recv");
exit(1);
}
if (!strcmp(RecvBuf.buf, "bye"))
{
close(*(int *)arg);
break;
}
//printf("Recv From Client %d : %s
", *(int *)arg, buf);
ret = send(RecvBuf.ToFd, &RecvBuf, sizeof(RecvBuf), 0);
if (-1 == ret)
{
perror("send");
exit(1);
}
memset(&RecvBuf, 0, sizeof(RecvBuf));
}
}
int main()
{
int sockfd, ret, fd[100] = {0}, length, i = 0;
char buf[100] = {0};
struct sockaddr_in server_addr;
struct sockaddr_in client_addr;
pthread_t tid[100] = {0};
printf("Start Server!
");
sockfd = socket(PF_INET, SOCK_STREAM, 0);
if (-1 == sockfd)
{
perror("socket");
exit(1);
}
int opt = 1;
setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt));
memset(&server_addr, 0, sizeof(server_addr));
server_addr.sin_family = PF_INET;
server_addr.sin_port = PORT;
server_addr.sin_addr.s_addr = inet_addr("127.0.0.1");
ret = bind(sockfd, (struct sockaddr *)&server_addr, sizeof(server_addr));
if (-1 == ret)
{
perror("bind");
exit(1);
}
printf("Listening...
");
ret = listen(sockfd, 5);
if(-1 == ret)
{
perror("listen");
exit(1);
}
while (1)
{
length = sizeof(client_addr);
fd[i] = accept(sockfd, (struct sockaddr *)&client_addr, &length);
if (-1 == fd[i])
{
perror("accept");
exit(1);
}
printf("Accept %d , Port %d
", fd[i], client_addr.sin_port);
ret = pthread_create(&tid[i], NULL, MyReceive, (void *)&fd[i]);
if (0 != ret)
{
perror("pthread_create");
exit(1);
}
i++;
}
close(sockfd);
return 0;
}
#include
#include
#include
#include
#include
#include
#include
#include
#define PORT 8888
struct info
{
int ToFd;
char buf[100];
};
pthread_t tid[2] = {0};
void *Send(void *arg)
{
struct info SendBuf = {0};
int ret;
while (1)
{
scanf("%s %d", SendBuf.buf, &SendBuf.ToFd);
ret = send(*(int *)arg, &SendBuf, sizeof(SendBuf), 0);
if (-1 == ret)
{
perror("send");
exit(1);
}
if (!strcmp(SendBuf.buf, "bye"))
{
close(*(int *)arg);
pthread_cancel(tid[1]);
break;
}
memset(&SendBuf, 0, sizeof(SendBuf));
}
}
void *Recv(void *arg)
{
struct info RecvBuf = {0};
int ret, oldtype;
pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, &oldtype);
while (1)
{
ret = recv(*(int *)arg, &RecvBuf, sizeof(RecvBuf), 0);
if (-1 == ret)
{
perror("recv");
exit(1);
}
printf("\t\t\t%s
", RecvBuf.buf);
memset(&RecvBuf, 0, sizeof(RecvBuf));
}
}
int main()
{
int sockfd, ret;
struct sockaddr_in server_addr;
sockfd = socket(PF_INET, SOCK_STREAM, 0);
if (-1 == sockfd)
{
perror("socket");
exit(1);
}
memset(&server_addr, 0, sizeof(server_addr));
server_addr.sin_family = PF_INET;
server_addr.sin_port = PORT;
server_addr.sin_addr.s_addr = inet_addr("127.0.0.1");
printf("Start Connecting...
");
ret = connect(sockfd, (struct sockaddr *)&server_addr, sizeof(server_addr));
if (-1 == ret)
{
perror("connect");
exit(1);
}
ret = pthread_create(&tid[0], NULL, Send, (void *)&sockfd);
if (0 != ret)
{
perror("pthread_create1");
exit(1);
}
ret = pthread_create(&tid[1], NULL, Recv, (void *)&sockfd);
if (0 != ret)
{
perror("pthread_create2");
exit(1);
}
pthread_join(tid[0], NULL);
pthread_join(tid[1], NULL);
close(sockfd);
return 0;
}
위의 프로그램 수정 버전:
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define PORT 8888
struct info
{
int ToFd;
char buf[100];
};
int main()
{
int sockfd, ret, fd[100] = {0}, length, i = 0, j;
struct sockaddr_in server_addr;
struct sockaddr_in client_addr;
fd_set ReadFd, TmpFd;
int MaxFd;
struct info RecvBuf;
printf("Start Server!
");
sockfd = socket(PF_INET, SOCK_STREAM, 0);
if (-1 == sockfd)
{
perror("socket");
exit(1);
}
int opt = 1;
setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt));
bzero(&server_addr, sizeof(server_addr));
server_addr.sin_family = PF_INET;
server_addr.sin_port =htons(PORT);
server_addr.sin_addr.s_addr = inet_addr("127.0.0.1");
ret = bind(sockfd, (struct sockaddr *)&server_addr, sizeof(server_addr));
if (-1 == ret)
{
perror("bind");
exit(1);
}
ret = listen(sockfd, 5);
if(-1 == ret)
{
perror("listen");
exit(1);
}
FD_ZERO(&ReadFd);
FD_ZERO(&TmpFd);
FD_SET(sockfd,&ReadFd);
MaxFd = sockfd;
while(1)
{
TmpFd = ReadFd;
ret = select(MaxFd + 1, &TmpFd, NULL, NULL, NULL);
if(-1 == ret)
{
perror("select");
exit(1);
}
if(FD_ISSET(sockfd,&TmpFd))
{
length = sizeof(client_addr);
fd[i] = accept(sockfd, (struct sockaddr *)&client_addr, &length);
if (-1 == fd[i])
{
perror("accept");
exit(1);
}
printf("Accept %d , Port %d
", fd[i], client_addr.sin_port);
FD_SET(fd[i],&ReadFd);
MaxFd = fd[i];
i++;
}
else
{
for(j = 0; j < i; i++)
{
if(FD_ISSET(fd[j],&TmpFd))
{
ret = recv(fd[j],&RecvBuf,sizeof(RecvBuf),0);
if(ret < 0)
{
perror("recv");
exit(1);
}
ret = send(RecvBuf.ToFd,&RecvBuf,sizeof(RecvBuf),0);
if(ret < 0)
{
perror("send");
exit(1);
}
}
}
}
}
return 0;
}
#include
#include
#include
#include
#include
#include
#include
#include
#define PORT 8888
struct info
{
int ToFd;
char buf[100];
};
pthread_t tid[2] = {0};
void *Send(void *arg)
{
struct info SendBuf = {0};
int ret;
while (1)
{
scanf("%s %d", SendBuf.buf, &SendBuf.ToFd);
ret = send(*(int *)arg, &SendBuf, sizeof(SendBuf), 0);
if (-1 == ret)
{
perror("send");
exit(1);
}
if (!strcmp(SendBuf.buf, "bye"))
{
close(*(int *)arg);
pthread_cancel(tid[1]);
break;
}
memset(&SendBuf, 0, sizeof(SendBuf));
}
}
void *Recv(void *arg)
{
struct info RecvBuf = {0};
int ret, oldtype;
pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, &oldtype);
while (1)
{
ret = recv(*(int *)arg, &RecvBuf, sizeof(RecvBuf), 0);
if (-1 == ret)
{
perror("recv");
exit(1);
}
printf("\t\t\t%s
", RecvBuf.buf);
memset(&RecvBuf, 0, sizeof(RecvBuf));
}
}
int main()
{
int sockfd, ret;
struct sockaddr_in server_addr;
sockfd = socket(PF_INET, SOCK_STREAM, 0);
if (-1 == sockfd)
{
perror("socket");
exit(1);
}
memset(&server_addr, 0, sizeof(server_addr));
server_addr.sin_family = PF_INET;
server_addr.sin_port = htons(PORT);
server_addr.sin_addr.s_addr = inet_addr("127.0.0.1");
printf("Start Connecting...
");
ret = connect(sockfd, (struct sockaddr *)&server_addr, sizeof(server_addr));
if (-1 == ret)
{
perror("connect");
exit(1);
}
ret = pthread_create(&tid[0], NULL, Send, (void *)&sockfd);
if (0 != ret)
{
perror("pthread_create1");
exit(1);
}
ret = pthread_create(&tid[1], NULL, Recv, (void *)&sockfd);
if (0 != ret)
{
perror("pthread_create2");
exit(1);
}
pthread_join(tid[0], NULL);
pthread_join(tid[1], NULL);
close(sockfd);
return 0;
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
다양한 언어의 JSONJSON은 Javascript 표기법을 사용하여 데이터 구조를 레이아웃하는 데이터 형식입니다. 그러나 Javascript가 코드에서 이러한 구조를 나타낼 수 있는 유일한 언어는 아닙니다. 저는 일반적으로 '객체'{}...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.