분류 | 게시판 |
베스트 |
|
유머 |
|
이야기 |
|
이슈 |
|
생활 |
|
취미 |
|
학술 |
|
방송연예 |
|
방송프로그램 |
|
디지털 |
|
스포츠 |
|
야구팀 |
|
게임1 |
|
게임2 |
|
기타 |
|
운영 |
|
임시게시판 |
|
옵션 |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 | #include "headerFiles.h" int main(void) { int s; int len; char buffer[256]; struct sockaddr_in servAddr; struct sockaddr_in clntAddr; int clntAddrLen; memset(&servAddr, 0, sizeof(servAddr)); servAddr.sin_family = AF_INET; servAddr.sin_port = htons(12345); servAddr.sin_addr.s_addr = htonl(INADDR_ANY); printf("domain complet!\n"); if((s = socket(PF_INET, SOCK_DGRAM, 0)) < 0) { perror("Error: socket failed!"); exit(1); } if((bind(s, (struct sockaddr*)&servAddr, sizeof(servAddr))) < 0) { perror("Error: bind failed!"); exit(1); } printf("bind complete!\n"); for(int i = 0;; i++) { len = recvfrom(s, buffer, sizeof(buffer), 0, (struct sockaddr*)&clntAddr, &clntAddrLen); if (len == 1) break; printf("%d th - %s\t%d", i + 1, inet_ntoa(clntAddr.sin_addr), ntohs(clntAddr.sin_port)); sendto(s, buffer, len, 0, (struct sockaddr*)&clntAddr, sizeof(clntAddr)); printf(" - send\n"); } printf("Server close!\n"); close(s); return 0; } | cs |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 | #include "headerFiles.h" #define IP "127.0.0.1" int main(int argc, char* argv[]) { int s; int len; int servName; short servPort; char *string; char buffer[256 + 1]; struct sockaddr_in servAddr; servName = inet_addr(IP); servPort = htons(12345); string = argv[1]; memset(&servAddr, 0, sizeof(servAddr)); servAddr.sin_family = AF_INET; servAddr.sin_addr.s_addr = servName; servAddr.sin_port = servPort; if((s = socket(PF_INET, SOCK_DGRAM, 0)) < 0) { perror("Error: Socket failed!"); exit(1); } printf("New socket!\n"); if ((len = sendto(s, string, strlen(string), 0, (struct sockaddr*)&servAddr, sizeof(servAddr))) < 0) { perror("Error: Sending failed!"); exit(1); } printf("Sending!\n"); if (len == 1) { printf("Close!\n"); close(s); exit(0); } if (recvfrom(s, buffer, len, 0, NULL, NULL) < 0) { perror("Error: Receive failed!"); exit(1); } printf("Receiving!\n"); buffer[len] = '\0'; printf("Echo string received: "); fputs(buffer, stdout); printf("\n"); close(s); return 0; } | cs |
죄송합니다. 댓글 작성은 회원만 가능합니다.