linux编程socket程序的编译语句
Linux\u4e0bC\u8bed\u8a00socket\u7f16\u7a0bsocket\u7f16\u7a0b\u4e00\u822c\u662f\u57fa\u4e8etcp\u6216\u8005udp\u534f\u8bae\u6765\u5199\u7684\uff0c\u4f60\u7684\u95ee\u9898\u5f88\u62bd\u8c61\uff0c\u6211\u4e0d\u77e5\u9053\u4f60\u8981\u7684\u662f\u57fa\u4e8etcp\u8fd8\u662fudp\u7684socket\u7f16\u8bd1\u3002\u6211\u628a\u76f8\u5bf9\u96be\u7684\u57fa\u4e8etcp\u534f\u8bae\u7684socket\u7f16\u8bd1\u7ed9\u4f60\u5427\u3002\u4f60\u60f3\u770b\u61c2\u6211\u7684\u4ee3\u7801\u9700\u8981\u77e5\u9053tcp\u7684\u4e09\u6b21\u63e1\u624b\u673a\u5236\u3002\u5426\u5219\u6211\u5199\u4e86\u6ce8\u91ca\uff0c\u4f60\u770b\u4ee3\u7801\u4e5f\u6709\u70b9\u56f0\u96be\u3002 ////\u670d\u52a1\u7aef\u7684\u4ee3\u7801
1 #include
2 #include
3 #include
4 #include
5 #include
6 #include
7 #include
8 #include
9
10 #define LOCAL_PORT 1234
11 #define MAX_LEN 512
12 #define MAX_NUM 5
13
14 int main(int argc, char *argv[])
15 {
16 int sock_fd, sock_data;
17 int ret, len_addr;
18 char buf[MAX_LEN];
19 ssize_t len;
20 struct sockaddr_in local_addr, remote_addr;
21
22 sock_fd = socket(AF_INET, SOCK_STREAM, 0); //\u521b\u5efa\u5957\u63a5\u5b57\uff0csock_fd\u662f\u5957\u63a5\u5b57\u63cf\u8ff0\u7b26\uff0c\u7c7b\u4f3c\u6211\u4eec\u7684\u8eab\u4efd\u8bc1\u53f7\u7801
23 if (sock_fd < 0)
24 {
25 perror("socket()");
26 return sock_fd;
27 }
28
29 local_addr.sin_family = AF_INET;// \u534f\u8bae\u65cf\uff0cipv4
30 local_addr.sin_port = htons(LOCAL_PORT);// \u628a\u670d\u52a1\u5668\u7aef\u53e3\u8f6c\u6362\u6210\u7f51\u7edc\u5b57\u8282\u5e8f
31 local_addr.sin_addr.s_addr = inet_addr("127.0.0.1");//\u628a\u5b57\u7b26\u4e32\u5f62\u5f0f\u7684ip\u8f6c\u6362\u6210\u7f51\u7edc\u5b57\u8282\u5e8f
32
33 ret = bind(sock_fd, (struct sockaddr *)&local_addr, (size_t)sizeof(local_addr));// \u628asock_fd\u548c\u672c\u673aip\uff0c\u7aef\u53e3\u90a6\u5b9a
34 if (ret < 0)
35 {
36 perror("bind()");
37 close(sock_fd);
38 return ret;
39 }
40
41 ret = listen(sock_fd, MAX_NUM);//\u76d1\u542csocket
42 if (ret)
43 {
44 perror("listen()");
45 close(sock_fd);
46 return ret;
47 }
48
49 memset(buf, 0, MAX_LEN);
50
51 len_addr = sizeof(remote_addr);
52
53 sock_data = accept(sock_fd, (struct sockaddr *)&remote_addr, (socklen_t *)&len_addr);//\u63a5\u53d7\u5ba2\u6237\u7aef\u7684\u8fde\u63a5
54 if (ret < 0)
55 {
56 perror("accept()");
57 close(sock_fd);
58 return ret;
59 }
60
61 while (1)
62 {
63 int slen;
64 len = recv(sock_data, buf, MAX_LEN, 0);//\u63a5\u53d7\u5ba2\u6237\u7aef\u7684\u6570\u636e
65 if (len < 0)
66 {
67 perror("recv()");
68 close(sock_data);
69 close(sock_fd);
70 return len;
71 }
72 printf("%s\n", buf);
73
74 slen = send(sock_data, "congratulation!", 15, 0);//\u5411\u5ba2\u6237\u7aef\u53d1\u9001\u6570\u636e
75 if (slen <= 0)
76 {
77 printf("slen = %d\n", slen);
78 perror("send()");
79 close(sock_data);
80 close(sock_fd);
81 return slen;
82 }
83 sleep(1);
84 }
85
86 close(sock_data);
87 close(sock_fd);
88
89 return 0;
90 }
////////////\u5ba2\u6237\u7aef\u7684\u4ee3\u7801 1 #include
2 #include
3 #include
4 #include
5 #include
6 #include
7 #include
8 #include
9
10 #define REMOTE_PORT 1234
11 #define MAX_LEN 512
12
13 int main(int argc, char *argv[])
14 {
15 int sock_fd, ret;
16 int len;
17 char buf[MAX_LEN];
18 struct sockaddr_in local_addr, remote_addr;
19
20 sock_fd = socket(AF_INET, SOCK_STREAM, 0);
21 if (sock_fd < 0)
22 {
23 perror("socket()");
24 return sock_fd;
25 }
26
28 local_addr.sin_family = AF_INET;
29 local_addr.sin_addr.s_addr = htonl(INADDR_ANY); //\u81ea\u52a8\u83b7\u53d6\u672c\u673a\u7684ip\u5730\u5740
30 local_addr.sin_port = htons(0); //\u968f\u673a\u9009\u53d6\u53ef\u7528\u7684\u7aef\u53e3\uff0c\u5e76\u4e0d\u662f\u6307\u5b9a\u7aef\u53e3\u4e3a0
31
33 remote_addr.sin_family= AF_INET;
34 remote_addr.sin_port = htons(REMOTE_PORT);
35 ret = inet_aton("127.0.0.1", &remote_addr.sin_addr);
36
38 ret = bind(sock_fd, (struct sockaddr *)&local_addr, sizeof(local_addr)); //\u628a\u672c\u673a\u7684ip,port\u548csocket\u7ed1\u5b9a
39 if (ret < 0)
40 {
41 perror("bind() !");
42 close(sock_fd);
43 return ret;
44 }
45
47 ret = connect(sock_fd, (struct sockaddr *)&remote_addr, (socklen_t)sizeof(remote_addr)); //\u628a\u672c\u673a\u7684socket\u548c\u5bf9\u65b9\u7684port\uff0cip\u5efa\u7acb\u8fde\u63a5
48 if (ret < 0)
49 {
50 perror("connect()");
51 close(sock_fd);
52 return ret;
53 }
54
55 memset(buf, 0, MAX_LEN);
56
57 while (1)
58 {
59 int i;
60 // len = send(sock_fd, buf, (size_t)MAX_LEN, 0);
61 len = send(sock_fd, "hello", 6, 0);
62 if (len <= 0)
63 {
64 perror("send()");
65 close(sock_fd);
66 return ret;
67 }
68
69 // printf("%d-->bytes send!\n", len);
70 sleep(1);
71
72 len = recv(sock_fd, buf, MAX_LEN, 0);
73 if (len <= 0)
74 {
75 perror("recv()");
76 close(sock_fd);
77 return ret;
78 }
79
80 for (i = 0; i < len; i++)
81 {
82 printf("%c", buf[i]);
83 }
84 printf("\n");
85 }
86
87 close(sock_fd);
88
89 return 0;
90 }
\u4f60\u628a\u670d\u52a1\u7aef\u548c\u5ba2\u6237\u7aef\u8fd9\u4e24\u4e2a\u7a0b\u5e8f\u5206\u522b\u4fdd\u5b58\u4e3aserver.c\u548cclient.c\u3002\u7136\u540e\u7f16\u8bd1gcc server.c -o server\uff0cgcc client .c -o client\u3002\u8fd0\u884c\u65f6\u5148\u8fd0\u884c\u670d\u52a1\u7aef\uff0c\u7528\u547d\u4ee4./server\uff0c\u518d\u8fd0\u884c\u5ba2\u6237\u7aef\uff0c\u7528\u547d\u4ee4./client\u3002 \u6ce8\u610f\u8fd0\u884c\u547d\u4ee4\u662f\u201c\u70b9 \u659c\u6760\u201d\uff0c\u201c\u70b9\u201d\u8868\u793a\u5f53\u524d\u76ee\u5f55\u3002
linux\u4e0b\u6ca1\u5b89\u88c5\u7f16\u8bd1\u73af\u5883\u5417\uff1f\u7528 gcc\u554a
server端:#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include<string.h>
#include <ctype.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#define MAX_LINE 100
int main()
{
struct sockaddr_in server;
struct sockaddr_in client;
int server_fd;
int client_fd;
socklen_t len;
char buf[MAX_LINE]; //用于存储传送内容到缓冲区
char addr_ip[INET_ADDRSTRLEN];//存储客户端地址缓冲区
int port = 8000;//端口号8000
int n; //读写字节数
bzero(&server,sizeof(server));//清空地址结构
server.sin_family = AF_INET;//使用ipv4
server.sin_addr.s_addr = INADDR_ANY;//服务器可以接受任意地址
server.sin_port = htons(port);//把端口号转成网络字节序
server_fd = socket(AF_INET,SOCK_STREAM,0);//创立套接字,使用tcp协议
bind(server_fd,(struct sockaddr *)&server,sizeof(server));//将地址和套接字绑定
listen(server_fd,10);//开始监听
printf("waiting```\n");
while(1)
{
client_fd = accept(server_fd,(struct sockaddr *)&client,&len);
n = read(client_fd,buf,MAX_LINE);//读取客户端来的消息
inet_ntop(AF_INET,&client.sin_addr,addr_ip,sizeof(addr_ip));
printf("client ip is : %s , port is : %d\n",addr_ip,ntohs(client.sin_port));
printf("content is : %s\n",buf);//打印客户端信息
write(client_fd,buf,n);//转发回去给客户端
close(client_fd);
}
if(close(client_fd) == -1)
{
perror("fail to close");
exit(1);
}
return 0;
}
client端:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#define MAX_LINE 100
int main(int argc,char * argv[])
{
struct sockaddr_in server;//服务器地址
char buf[MAX_LINE];
int server_fd;
int port = 8000;
char *str = "test string";//默认字符串
if(argc > 1)
{
str = argv[1];//从命令参数中取得用户输入的串
}
bzero(&server,sizeof(server));//清空地址结构
server.sin_family = AF_INET;//使用ipv4
inet_pton(AF_INET,"192.168.228.129",&server.sin_addr);
server.sin_port = htons(port);
server_fd = socket(AF_INET,SOCK_STREAM,0);//创建套接字
connect(server_fd,(struct sockaddr *)&server,sizeof(server));
write(server_fd,str,strlen(str) + 1);
//因为是本机测试,所以不用考虑延时
read(server_fd,buf,MAX_LINE);
printf("recieve from server :%s\n",buf);
close(server_fd);
return 0;
}
//源码来自于网络,谁写也都类似,都是基于tcp的三次握手协议来写的
绛旓細鏈鍚庯紝TCP杩炴帴鐨勫缓绔嬪拰鍏抽棴閬靛惊涓ユ牸鐨勫洓娆℃尌鎵嬪崗璁細涓夋鎻℃墜寤虹珛杩炴帴锛屽洓娆℃尌鎵嬬‘淇濇暟鎹紶杈撳畬鎴愩傚湪鏈嶅姟鍣ㄧ锛屼竴涓ず渚绋嬪簭鍙兘鍖呮嫭鐩戝惉鐗瑰畾绔彛銆佹帴鍙楄繛鎺ヨ姹傚苟浜や簰鏁版嵁锛屽悓鏃跺鐞嗗彲鑳界殑TIME_WAIT鐘舵侊紝纭繚鏁版嵁浼犺緭鐨勫畬鏁存у拰鍙潬鎬с傛棤璁烘槸瀹㈡埛绔繕鏄湇鍔″櫒锛Socket缂栫▼鐨勫疄璺甸兘闇瑕佹帉鎻¤繖浜涙牳蹇冩蹇点傞...
绛旓細TCP:(Transfer Control Protocol)浼犺緭鎺у埗鍗忚鏄竴绉嶉潰鍚戣繛鎺ョ殑鍗忚, 褰撴垜浠殑缃戠粶绋嬪簭浣跨敤杩欎釜鍗忚鐨勬椂鍊,缃戠粶鍙互淇濊瘉鎴戜滑鐨勫鎴风鍜屾湇鍔$涔嬮棿鐨勪紶杈撴槸鍙潬鐨勩 UDP:(User Datagram Protocol)鐢ㄦ埛鏁版嵁鎶ュ崗璁槸涓绉嶉潪闈㈠悜杩炴帴鐨勫崗璁, 杩欑鍗忚骞朵笉鑳戒繚璇佹垜浠殑缃戠粶绋嬪簭鐨勮繛鎺ユ槸鍙潬鐨勩 鎴戜滑缂栧啓鐨勭綉缁滅▼搴忓叿浣撻噰鐢ㄥ摢...
绛旓細Linux缃戠粶妗嗘灦涓殑Socket缂栫▼鏄牳蹇冩蹇碉紝瀹冨畾涔変簡閫氫俊绔偣鍜岀綉缁滆繛鎺ョ殑鎺ュ彛銆侺inux鍐呮牳閫氳繃涓濂楁爣鍑嗙殑System call鎻愪緵浜嗕竴鏁村鐢ㄦ埛绌洪棿缃戠粶搴旂敤寮鍙戝伐鍏凤紝濡俉eb鏈嶅姟鍣ㄣ侀偖浠舵湇鍔″櫒鍜屾枃浠舵湇鍔″櫒绛夈係ocket缂栫▼涓昏鐢变笁涓叧閿ā鍧楁瀯鎴愶細BSD Socket APIs銆丼ocket鎶借薄灞備互鍙奝rotocol Family銆侭SD Socket APIs鏄敤鎴风┖...
绛旓細Linux Socket缂栫▼涓殑I/O澶嶇敤鏈哄埗鏄湇鍔″櫒璁捐涓殑鍏抽敭浼樺寲鎵嬫锛屽畠閫氳繃绯荤粺鍐呮牳鐨勯珮鏁堢鐞嗭紝鍏佽鍗曚釜杩涚▼鍚屾椂鐩戞帶澶氫釜鏂囦欢鎻忚堪绗︼紝涓鏃︽煇涓弿杩扮鍑嗗濂借繘琛岃鍐欙紝绋嬪簭鍗冲彲鍝嶅簲銆備紶缁熺殑闃诲寮忓鎺ュ瓧缂栫▼鍦ㄩ珮骞跺彂鍦烘櫙涓嬫晥鐜囦綆涓嬶紝鍥犱负涓涓嚎绋嬩細琚崟涓狪/O鎿嶄綔闃诲锛屾棤娉曞鐞嗗叾浠栬姹傘侷/O澶氳矾澶嶇敤濡俿elect...
绛旓細鍙兘闇瑕佹鏌socket鐘舵佽屼笉鏄畝鍗曞湴閲嶆柊璋冪敤銆傛荤殑鏉ヨ锛屽鐞唀intr鍜宔again鐨勫叧閿湪浜庣悊瑙e畠浠殑鍚箟锛岄夋嫨鍚堥傜殑绛栫暐銆傚湪缂栫▼瀹炶返涓紝鎴戜滑搴斾紭鍏堟墜鍔ㄥ鐞唀intr锛屽悓鏃跺湪闈為樆濉炴ā寮忎笅閫傚綋澶勭悊eagain锛屼互纭繚搴旂敤绋嬪簭鐨勭ǔ瀹氭у拰鍝嶅簲鎬с傝鎴戜滑鍦Linux鐨剆ocket涓栫晫涓紝浼橀泤鍦板簲瀵硅繖浜涢敊璇爜锛屾彁鍗囩▼搴忕殑鍋ュ.鎬с
绛旓細linux涓锛屾湭鑳芥垚鍔熸墽琛岀殑socket鎿嶄綔浼氳繑鍥-1锛涘鏋滃寘鍚簡errno.h锛屽氨浼氳缃甧rrno鍙橀噺 6. 璁剧疆闈為樆濉 windows涓媔octlsocket()linux涓媐cntl()锛岄渶瑕佸ご鏂囦欢fcntl.h 7. send鍑芥暟鏈鍚庝竴涓弬鏁 windows涓嬩竴鑸缃负0 linux涓嬫渶濂借缃负MSG_NOSIGNAL锛屽鏋滀笉璁剧疆锛屽湪鍙戦佸嚭閿欏悗鏈夊彲鑳戒細瀵艰嚧绋嬪簭閫鍑 8. 姣绾...
绛旓細linuxsocket榛樿瓒呮椂鏃堕棿锛熶妇渚嬶細s=socket();璁剧疆s涓簄on-blocking锛沜onnect(s,..);FD_SET...;rc=select(...,10s);if(rc==0)琛ㄧず10s瓒呮椂浜嗐傝繖涓秴鏃剁殑鎰忔濇槸锛10s涔嬪唴锛宻elect涓墍鏈socket鐨浜嬩欢鍧囨湭浜х敓锛堝鏋滆嚦灏戞湁涓涓骇鐢燂紝鍒檙c澶т簬0)娉ㄦ剰锛氳繖涓10s璺焎onnect鏈韩鐨勮秴鏃舵満鍒跺畬鍏ㄦ棤鍏筹紝鍓嶈呯殑...
绛旓細windows socket鍜宭inux socket缂栫▼鐨勫尯鍒富瑕佸湪浜巗ocket 鍑芥暟搴撶殑鍖哄埆涓婏細1)澶存枃浠 windows涓媤insock.h/winsock2.h linux涓sys/socket.h 閿欒澶勭悊锛歟rrno.h 鍏朵粬甯哥敤鍑芥暟鐨勫ご鏂囦欢鍙埌鍛戒护琛屼笅鐢╩an鎸囦护鏌ヨ銆2)鍒濆鍖 windows涓嬮渶瑕佺敤WSAStartup linux涓嬩笉闇瑕侊紙寰堟柟渚匡級锛岀洿鎺ュ彲浠ヤ娇鐢 3)鍏抽棴socket win...
绛旓細server绔細listen();accept();鍚姩鏂扮嚎绋嬪鐞哸ccpet鐨socket锛屾敹鍙戞暟鎹紝鐒跺悗缁х画accept锛岃繖鏍峰彲浠ュ鐞嗗苟鍙戣姹傘俢lient绔:connect() server锛屾敹鍙戞暟鎹
绛旓細Socket閫氫俊鍒涘缓姝ラ锛氾紙1锛夐氳繃socket()鍑芥暟鍒涘缓socket锛2锛夐氳繃bind鍑芥暟缁戝畾socket浜庤澶囧湴鍧锛3锛夎繘琛岃鍐欐搷浣渞ead/recv/recvfrom write/send/sendto锛4锛塩lose鏂规硶鍏抽棴濂楁帴瀛 渚嬪瓙濡備笅锛歵est1.c include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <netinet/in.h> #...