ECHO サーバに、"hello" を送信し、返信された内容を標準出力に書き出すプログラムをつくりました。
エラーしょりはじゅうぶんにかけていますでしょうか。
c
1root@localhost:~ # vi kadai1-4_1.c 2#include <sys/types.h> 3#include <sys/socket.h> 4#include <netinet/in.h> 5#include <netdb.h> 6#include <stdio.h> 7#include <stdlib.h> 8#include <errno.h> 9#include <err.h> 10#include <unistd.h> 11#include <string.h> 12 13int main(){ 14 struct addrinfo hints, *res; 15 int l, s; 16 int error; 17 char buf[1024]; 18 19 memset(&hints, 0, sizeof(hints)); 20 hints.ai_socktype = SOCK_STREAM; 21 22 error=getaddrinfo("10.50.18.200","7",&hints, &res); 23 if(error!=0){ 24 errx(1,"%s",gai_strerror(error)); 25 } 26 while(res!=NULL){ 27 s = socket(res->ai_family, res->ai_socktype, res->ai_protocol); 28 if(s<0){ 29 res=res->ai_next; 30 } 31 else{ 32 break; 33 } 34 } 35 if(connect(s, res->ai_addr, res->ai_addrlen)<0){ 36 close(s); 37 } 38 snprintf(buf, sizeof(buf), "%s", "helo\n"); 39 write(s, buf, strlen(buf)+1); 40 while((l=read(s,buf,sizeof(buf)))>0){ 41 write(STDOUT_FILENO, buf, l); 42 } 43 close(s); 44} 45
回答2件
あなたの回答
tips
プレビュー