socket 리셋 서버

server.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);
//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; }

좋은 웹페이지 즐겨찾기