Computer Networking Homework1

看板HSNU_979作者 (漂泊的人)時間20年前 (2004/03/17 10:35), 編輯推噓1(100)
留言1則, 1人參與, 最新討論串1/6 (看更多)
※ [本文轉錄自 NTUBA95study 看板] 作者: leofersten (決心之後) 看板: NTUBA95study 標題: Computer Networking Homework1 時間: Tue Mar 16 13:45:12 2004 #include <stdio.h> #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #include <netdb.h> #define SERVER_PORT 5432 #define MAX_LINE 256 int main(int argc, char * argv[]) { FILE *fp; struct hostent *hp; struct sockaddr_in sin; char *host char buf[MAX_LINe]; int s; int len; if (argc==2) { host = argv[1]; } else { fprintf(stderr, "usage: simplex-talk host\n"); exit(1); } /* translate host name into peer's IP address */ hp = gethostbyname(host); if (!hp) { fprintf(stderr, "simplex-talk: unknown host: %s\n", host); exit(1) } /* build address data structure */ bzero((char *)&sin, sizeof(sin)); sin.sin_family = AF_INET; bcopy(hp->h_addr, (char *)&sin.sin_addr, hp->h_lenghth); sin.sin_port = htons(SERVER_PORT); /* active open */ if ((s = socket(PF_INET, SOCK_STREAM, 0)) < 0) { perror("simplex-talk: socket"); exit(1); } if (connect(s, (struct sockaddr *)&sin, sizeof(sin)) < 0) { perror("simplex-talk: connect"); close(s); exit(1); } /* main loop: get and send lines of text */ while (fgets(buf, sizeof(buf), stdin)) { buf[MAX_LINE-1] = '\0'; len = strlen(buf) + 1; send(s, buf, len, 0); } } ------------------------------------------------------------------------------ 以上為課本P.34 client端的原始code 也就是老師給的範例 however... 使用Visual C++ 把它compile 之後就會發現問題: fatal error C1083: Cannot open include file: 'sys/socket.h': No such file or directory Error executing cl.exe. 因為在程式碼最上面的#include裡用到Unix裡的Networking utility 也就是說在Visual C++ 內建的程式庫中找不到 sys/socket.h netinet/in.h netdb.h 所以也就沒辦法在Windows 的環境中執行這一個程式了...... 除非是在Unix中使用 所以要完成作業有幾個方法: 1.自己在Windows環境寫一個出來 (我請教過台大資工和清大資工大二的同學 他們也寫不出來... ^^") 2.找得到使用Unix(或Linux)為OS的電腦並且會使用Unix 就可以直接用書中範例 (一般PC都是使用Windows 真要在PC上使用Unix就要灌成雙OS 這也蠻難的...) 3.到Internet去找TCP通訊的聊天室原始碼 (可以用 "C語言" 或 "Socket Programming" 當關鍵字 去下載別人寫好的程式碼 這自然是比上兩個方法方便多了 ) 不過無論是要寫或是讀取 都要先安裝Visual C++ 或是 BCB -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 140.112.243.57

推140.112.242.231 03/16,
推資工也寫不出來...
推140.112.242.231 03/16

推 140.112.222.28 03/16,
我覺得這個可能要找交大電信的
推 140.112.222.28 03/16
-- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 210.192.240.85

推 140.113.126.79 03/23, , 1F
不好意思 我c++弱到噴
推 140.113.126.79 03/23, 1F
文章代碼(AID): #10LxbpAG (HSNU_979)
文章代碼(AID): #10LxbpAG (HSNU_979)