#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 1024
void error_handling(char *message);
int main(int argc, char **argv)
{
int sock;
char message[BUFSIZE];
int str_len;
struct sockaddr_in serv_addr;
if(argc!=3){
printf("Usage : %s <IP> <prot>\n",argv[0]);
exit(1);
}
sock=socket(PF_INET,SOCK_STREAM,0);
if(sock==-1)
error_handling("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]));
if(connect(sock, (struct sockaddr*)&serv_addr, sizeof(serv_addr))==-1)
error_handling("connect() error!");
while(1){
/*Enter and Send message*/
fputs("Enter Message (q to quit) : ",stdout);
fgets(message, BUFSIZE, stdin);
if(!strcmp(message,"q\n"))
break;
write(sock, message, strlen(message));
/*get message and print*/
str_len=read(sock, message,BUFSIZE-1);
message[str_len]=0;
printf("message : %s \n",message);
}
close(sock);
return 0;
}
void error_handling(char *message)
{
fputs(message,stderr);
fputc('\n',stderr);
exit(1);
}
'Security Study > Network' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
ssl server (0) | 2015.09.18 |
---|---|
echo UDP_client (0) | 2015.09.18 |
echo UDP_server (0) | 2015.09.18 |
echo TCP_server (0) | 2015.09.18 |
network (0) | 2015.09.14 |
๋๊ธ