Security Study/Network

echo UDP_client

๐“›๐“พ๐“ฌ๐“ฎ๐“ฝ๐“ฎ_๐“ข๐“ฝ๐“ฎ๐“ต๐“ต๐“ช 2015. 9. 18.
728x90
๋ฐ˜์‘ํ˜•

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

#include <unistd.h>

#include <arpa/inet.h>

#include <sys/types.h>

#include <sys/socket.h>


#define BUFSIZE 30

void error_handling(char *message);


int main(int argc, char **argv)

{

int sock;

char message[BUFSIZE];

int str_len, addr_size;


struct sockaddr_in serv_addr;

struct sockaddr_in from_addr;

int clnt_addr_size;


if (argc != 3){

printf("Usage : %s <IP>  <port>\n", argv[0]);

exit(1);

}


sock = socket(PF_INET, SOCK_DGRAM, 0);


if (sock == -1)

error_handling("UDP socket error");


memset(&serv_addr, 0, sizeof(serv_addr));

serv_addr.sin_family = AF_INET;

serv_addr.sin_addr.s_addr = inet_addr(argv[1]);

serv_addr.sin_port = htons(atoi(argv[2]));



while (1){

fputs("Enter message (q to quot) : ", stdout);

fgets(message, sizeof(message), stdin);

if (!strcmp(message, "q\n"))

break;


sendto(sock, message, strlen(message), 0, (struct sockaddr*)&serv_addr, sizeof(serv_addr));

addr_size = sizeof(from_addr);

str_len = recvfrom(sock, message, BUFSIZE, 0,(struct sockaddr*)&from_addr, &addr_size);


message[str_len] = 0;

printf("Message : %s", message);

}

close(sock);

return 0;

}

void error_handling(char *message)

{

fputs(message, stderr);

fputc('\n', stderr);

exit(1);

}



728x90
๋ฐ˜์‘ํ˜•

'Security Study > Network' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

ssl client  (0) 2015.09.18
ssl server  (0) 2015.09.18
echo UDP_server  (0) 2015.09.18
echo TCP_client  (0) 2015.09.18
echo TCP_server  (0) 2015.09.18

๋Œ“๊ธ€