TCP 및 UDP 편집 프로세스 및 코드
6320 단어 네트워크 통신
TCP 편집 프로세스:
서버 측:
클라이언트:
TCP는 각 인터페이스 함수를 프로그래밍합니다.
socket 만들기
int socket(int domain, int type, int protocol);
명령 소켓
int bind(int sockfd, const struct sockaddr *addr, socklen_t addrLen);
소켓 감청
int listen(int sockfd, int backlog);
링크 수신
int accept(int sockfd, struct sockaddr *addr, socklen_t *addrLen);
시작 링크
int connect(int sockfd, struct sockaddr *serAddr, socklen_t addrLen);
데이터 읽기 및 쓰기
데이터 읽기
int recv(int sockfd, void *buff, size_t buffSize, int flag);
데이터를 쓰다
int write(int sockfd, const void *buff, size_t dataLen, int flag);
연결 해제
int close(int sockfd);
UDP 편집 프로세스
서버 측:
클라이언트:
UDP 데이터 읽기 및 쓰기 인터페이스 함수
데이터 읽기
int recvfrom(int sockfd, void *buff, size_t buffSize, int flag, struct sockaddr *peerAddr, socklen_t *addrLen);
데이터를 쓰다
int sendto( int sockfd, void *buff, size_t dataLen, int flag, struct sockaddr *destAddr, socklen_t addrLen);
TCPServer 코드는 다음과 같습니다.
#include
#include
#include
#include
#include
#include
#include
#include // IP
#include //
int main()
{
//
int listenfd = socket(AF_INET, SOCK_STREAM, 0);
assert(-1 != listenfd);
struct sockaddr_in ser, cli;
memset(&ser, 0, sizeof(ser));
ser.sin_family = AF_INET; //
ser.sin_port = htons(6000);
inet_aton("127.0.0.1", (struct in_addr*)&ser.sin_addr);
int res = bind(listenfd, (struct sockaddr*)&ser, sizeof(ser));
assert(-1 != res);
listen(listenfd, 2);
while(1)
{
int len = sizeof(cli);
// c
// accept
int c = accept(listenfd, (struct sockaddr*)&cli, &len); //
assert(-1 != c);
while(1)
{
char buff[128] = {0};
int n = recv(c, buff, 2, 0); //
if(n == 0)
{
printf("client unlink
");
close(c);
break;
}
else if(n == -1)
{
printf("error
");
close(c);
break;
}
printf("n == %d: %s
", n, buff);
send(c, "OK", 2, 0);
}
}
close(listenfd);
}
TCPClient 코드는 다음과 같습니다.
#include
#include
#include
#include
#include
#include
#include
#include // IP
#include //
int main()
{
int sockfd = socket(AF_INET, SOCK_STREAM, 0);
assert(-1 != sockfd);
struct sockaddr_in ser;
memset(&ser, 0, sizeof(ser));
ser.sin_family = AF_INET;
ser.sin_port = htons(6000);
inet_aton("127.0.0.1", (struct in_addr*)&ser.sin_addr);
int res = connect(sockfd, (struct sockaddr*)&ser, sizeof(ser));
assert(-1 != res);
while(1)
{
printf("please input: ");
char data[128] = {0};
fgets(data, 128, stdin);
if(strncmp(data, "bye", 3) == 0)
{
close(sockfd);
break;
}
send(sockfd, data, strlen(data) - 1, 0);
char buff[128] = {0};
int n = recv(sockfd, buff, 127, 0);
if(n <= 0)
{
printf("error
");
close(sockfd);
break;
}
printf("n == %d: %s
", n, buff);
}
}
UDPServer 코드는 다음과 같습니다.
#include
#include
#include
#include
#include
#include
#include
#include
#include
int main()
{
int sockfd = socket(AF_INET, SOCK_DGRAM, 0);
assert(-1 != sockfd);
struct sockaddr_in ser;
memset(&ser, 0, sizeof(ser));
ser.sin_family = AF_INET;
ser.sin_port = htons(6500);
ser.sin_addr.s_addr = inet_addr("127.0.0.1");
int res = bind(sockfd, (struct sockaddr*)&ser, sizeof(ser));
while(1)
{
char buff[128] = {0};
struct sockaddr_in cli;
int len = sizeof(cli);
int n = recvfrom(sockfd, buff, 10, 0, (struct sockaddr*)&cli, &len);
if(n <= 0)
{
printf("error
");
continue;
}
printf("ip: %s, port: %d
", inet_ntoa(cli.sin_addr), ntohs(cli.sin_port));
printf("n == %d: %s
", n, buff);
sendto(sockfd, "OK", 2, 0, (struct sockaddr*)&cli, sizeof(cli));
}
}
UDPClient 코드는 다음과 같습니다.
#include
#include
#include
#include
#include
#include
#include
#include
#include
int main()
{
int sockfd = socket(AF_INET, SOCK_DGRAM, 0);
assert(-1 != sockfd);
struct sockaddr_in ser;
memset(&ser, 0, sizeof(ser));
ser.sin_family = AF_INET;
ser.sin_port = htons(6500);
ser.sin_addr.s_addr = inet_addr("127.0.0.1");
while(1)
{
printf("please input: ");
char data[128] = {0};
fgets(data, 128, stdin);
if(strncmp(data, "bye", 3) == 0)
{
close(sockfd);
break;
}
sendto(sockfd, data, strlen(data) - 1, 0,
(struct sockaddr*)&ser, sizeof(ser));
char buff[128] = {0};
int n = recvfrom(sockfd, buff, 127, 0, NULL, NULL);
if(n <= 0)
{
printf("error
");
break;
}
printf("n == %d: %s
", n, buff);
}
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
TCP 및 UDP 편집 프로세스 및 코드socket - 클라이언트 연결을 감청하는 데 사용할 소켓을 만듭니다 bind - 감청 소켓을 서버 IP 주소 및 포트 번호와 연결합니다 listen - 감청 시작 accept - 클라이언트 연결을 가져오고 클라이언...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.