socket 리셋 서버
4690 단어 linux 네트워크 프로그래밍
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
/*
int socket(int domain, int type, int protocol);
*/
/*
int bind(int sockfd, const struct sockaddr *my_addr, socklen_t addrlen);
//man 2 bind
struct sockaddr {
sa_family_t sa_family;
char sa_data[14];
}
//man 7 ip
struct sockaddr_in {
sa_family_t sin_family; // address family: AF_INET
u_int16_t sin_port; // port in network byte order
struct in_addr sin_addr; // internet address
};
//Internet address.
struct in_addr {
u_int32_t s_addr; //address in network byte order
};
*/
//int listen(int sockfd, int backlog);
//int accept(int sockfd, struct sockaddr *addr, socklen_t *addrlen);
/*
On success, accept() returns a non-negative integer that is a descriptor for the accepted socket.
On error, -1 is returned, and errno is set appropriately.
*/
int main()
{
int sockfd = 0;
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd == -1) {
perror("fun socket");
exit(0);
}
struct sockaddr_in srvaddr;
srvaddr.sin_family = AF_INET;
srvaddr.sin_port = htons(8001);
//srvaddr.sin_addr.s_addr = inet_addr("109.123.120.107"); //127.0.0.1
srvaddr.sin_addr.s_addr = (INADDR_ANY);
if (bind(sockfd, (struct sockaddr *) &srvaddr, sizeof(srvaddr)) < 0) {
perror("fun bind");
exit(0);
}
if (listen(sockfd, SOMAXCONN) < 0) {
perror("fun listen");
exit(0);
}
struct sockaddr_in peeraddr;
socklen_t peerlen = sizeof(peeraddr);
unsigned int conn = 0;
conn = accept(sockfd, (struct sockaddr *) &peeraddr, (socklen_t *) &peerlen);
if (conn == -1) {
perror("fun listen");
exit(0);
}
printf("perradd:%s
perrport:%d
", inet_ntoa(peeraddr.sin_addr), ntohs(peeraddr.sin_port));
char revbuf[1024] = { 0 };
while (1) {
int ret = read(conn, revbuf, sizeof(revbuf));
if (ret == 0) {
printf("client closed
");
exit(0);
} else if (ret < 0) {
perror("read");
exit(0);
}
//ssize_t write(int fd, const void *buf, size_t count);
fputs(revbuf, stdout);
write(conn, revbuf, ret);
memset(revbuf,0x00,sizeof(revbuf));
}
return 0;
}
client.c
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
/*
int socket(int domain, int type, int protocol);
*/
/*
int bind(int sockfd, const struct sockaddr *my_addr, socklen_t addrlen);
//man 2 bind
struct sockaddr {
sa_family_t sa_family;
char sa_data[14];
}
//man 7 ip
struct sockaddr_in {
sa_family_t sin_family; // address family: AF_INET
u_int16_t sin_port; // port in network byte order
struct in_addr sin_addr; // internet address
};
//Internet address.
struct in_addr {
u_int32_t s_addr; //address in network byte order
};
*/
//int listen(int sockfd, int backlog);
//accept ,
/* int accept(int sockfd, struct sockaddr *addr, socklen_t *addrlen);
On success, accept() returns a non-negative integer that is a descriptor for the accepted socket. On error,
-1 is returned, and errno is set appropriately.
int conn = 0;
*/
int main()
{
int sockfd = 0;
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd == -1) {
perror("fun socket
");
exit(0);
}
struct sockaddr_in srvaddr;
srvaddr.sin_family = AF_INET;
srvaddr.sin_port = htons(8001);
srvaddr.sin_addr.s_addr = inet_addr("109.123.120.107"); //127.0.0.1
//srvaddr.sin_addr.s_addr = inet_addr(INADDR_ANY); //
if (connect(sockfd, (struct sockaddr*) (&srvaddr), sizeof(srvaddr)) < 0) {
perror("fun socket
");
exit(0);
}
printf("connect success
");
char revbuf[1024] = { 0 };
char sendbuf[1024] = { 0 };
while (fgets(sendbuf, sizeof(sendbuf), stdin) != NULL) {
//
write(sockfd, sendbuf, strlen(sendbuf));
//
read(sockfd, revbuf, sizeof(revbuf));
//
fputs(revbuf, stdout); // ,
memset(revbuf, 0, sizeof(revbuf));
memset(sendbuf, 0, sizeof(sendbuf));
}
close(sockfd);
return 0;
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
socket 리셋 서버server.c client.c...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.