锘??xml version="1.0" encoding="utf-8" standalone="yes"?>99精品国产综合久久久久五月天,久久91精品国产91,少妇久久久久久久久久http://m.shnenglu.com/hideto/zh-cnSat, 05 Jul 2025 11:37:53 GMTSat, 05 Jul 2025 11:37:53 GMT60鏈綆鍗曠殑Erlang Socket紼嬪簭http://m.shnenglu.com/hideto/archive/2008/08/08/58327.htmlHidetoHidetoFri, 08 Aug 2008 04:40:00 GMThttp://m.shnenglu.com/hideto/archive/2008/08/08/58327.htmlhttp://m.shnenglu.com/hideto/comments/58327.htmlhttp://m.shnenglu.com/hideto/archive/2008/08/08/58327.html#Feedback0http://m.shnenglu.com/hideto/comments/commentRss/58327.htmlhttp://m.shnenglu.com/hideto/services/trackbacks/58327.html
 1 -module(server).
 2 -export([start/0]).
 3 
 4 start() ->
 5     {ok,Listen}=gen_tcp:listen(2345,[binary,{packet,4},
 6                                             {reuseaddr,true},
 7                                             {active,true}]),
 8     {ok,Socket}=gen_tcp:accept(Listen),
 9     gen_tcp:close(Listen),
10     loop(Socket).
11 
12 loop(Socket) ->
13     receive
14         {tcp,Socket,Bin} ->
15             io:format("Server received binary = ~p~n",[Bin]),
16             Str=binary_to_term(Bin),
17             io:format("Server (unpacked) ~p~n",[Str]),
18             Reply= Str ++ "** MLGB!",
19             io:format("Server replying = ~p~n",[Reply]),
20             gen_tcp:send(Socket,term_to_binary(Reply)),
21             loop(Socket);
22         {tcp_closed,Socket} ->
23             io:format("Server socket closed~n")
24     end.
client.erl:
 1 -module(client).
 2 -export([start/1]).
 3 
 4 start(Str) ->
 5   {ok, Socket} = 
 6     gen_tcp:connect("localhost"2345, [binary, {packet, 4}]),
 7     ok = gen_tcp:send(Socket, term_to_binary(Str)),
 8     receive
 9       {tcp, Socket, Bin} ->
10         io:format("Client received binary = ~p~n", [Bin]),
11         Val = binary_to_term(Bin),
12         io:format("Client result = ~p~n", [Val]),
13         gen_tcp:close(Socket)
14     end.
緙栬瘧:
1 Eshell > c(server).
2 Eshell > c(client).
鍚姩server:
1 Eshell > server:start().
鍚姩client:
1 Eshell > client:start("sb").
server绔緭鍑?
1 Server received binary = <<131,107,0,2,115,98>>
2 Server (unpacked) "sb"
3 Server replying = "sb** MLGB!"
4 Server socket closed
client绔緭鍑?
1 Client received binary = <<131,107,0,10,115,98,42,42,32,77,76,71,66,33>>
2 Client result = "sb** MLGB!"


Hideto 2008-08-08 12:40 鍙戣〃璇勮
]]>
絎竴涓猚lient/server紼嬪簭http://m.shnenglu.com/hideto/archive/2008/08/06/58146.htmlHidetoHidetoWed, 06 Aug 2008 06:59:00 GMThttp://m.shnenglu.com/hideto/archive/2008/08/06/58146.htmlhttp://m.shnenglu.com/hideto/comments/58146.htmlhttp://m.shnenglu.com/hideto/archive/2008/08/06/58146.html#Feedback0http://m.shnenglu.com/hideto/comments/commentRss/58146.htmlhttp://m.shnenglu.com/hideto/services/trackbacks/58146.html
 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 #include <strings.h>
 4 #include <unistd.h>
 5 #include <sys/types.h>
 6 #include <sys/socket.h>
 7 #include <netinet/in.h>
 8 #include <arpa/inet.h>
 9 
10 #define PORT 8888
11 #define BACKLOG 10
12 
13 int main(int argc, char *argv[])
14 {
15   int listenfd, connectfd;
16   struct sockaddr_in server;
17   struct sockaddr_in client;
18   int sin_size;
19 
20   if((listenfd = socket(AF_INET, SOCK_STREAM, 0)) == -1){
21     perror("Creating socket failed.");
22     exit(0);
23   }
24 
25   int opt = SO_REUSEADDR;
26   setsockopt(listenfd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt));
27 
28   bzero(&server, sizeof(server));
29   server.sin_family = AF_INET;
30   server.sin_port = htons(PORT);
31   server.sin_addr.s_addr = htonl(INADDR_ANY);
32 
33   if (bind(listenfd, (struct sockaddr *)&server,
34       sizeof(struct sockaddr)) == -1){
35     perror("Bind error!");
36     exit(0);
37   }
38 
39   if (listen(listenfd,BACKLOG) == -1){
40     perror("Listen error!");
41     exit(0);
42   }
43 
44   while(1){
45     sin_size = sizeof(struct sockaddr_in);
46 
47     if((connectfd = accept(listenfd, (struct sockaddr *)&client, &sin_size)) == -1) {
48       perror("Accept error!");
49       break;
50     }
51 
52     printf("You got a connection from %s\n", inet_ntoa(client.sin_addr));
53 
54     send(connectfd, "Fucking client!\n"160);
55 
56     close(connectfd);
57   }
58 
59   close(listenfd);
60   exit(0);
61 }
client.c:
 1 #include <stdio.h>
 2 #include <unistd.h>
 3 #include <strings.h>
 4 #include <stdlib.h>
 5 #include <sys/types.h>
 6 #include <sys/socket.h>
 7 #include <netinet/in.h>
 8 #include <netdb.h>
 9 
10 #define PORT 8888
11 #define MAXDATASIZE 100
12 
13 int main(int argc, char *argv[])
14 {
15   int fd, numbytes;
16   char buf[MAXDATASIZE];
17   struct hostent *he;
18   struct sockaddr_in server;
19 
20   if (argc != 2) {
21     printf("Usage: %s <IP Address>\n",argv[0]);
22     exit(0);
23   } 
24 
25   if((he = gethostbyname(argv[1])) == NULL) {
26     printf("gethostbyname() error\n");
27     exit(0);
28   } 
29 
30   if((fd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
31     printf("socket() error\n");
32     exit(0);
33   } 
34 
35   bzero(&server, sizeof(server));
36   server.sin_family = AF_INET;
37   server.sin_port = htons(PORT);
38   server.sin_addr = *((struct in_addr *)he->h_addr);
39 
40   if(connect(fd, (struct sockaddr *)&server, sizeof(struct sockaddr)) == -1) {
41     close(fd);
42     printf("Connect error!");
43     exit(0);
44   }
45 
46   if((numbytes = recv(fd, buf, MAXDATASIZE, 0)) == -1) {  /* calls recv() */
47     printf("Recv error!");
48     exit(0);
49   }
50 
51   buf[numbytes] = '\0';
52   printf("Server Message: %s \n", buf);
53 
54   close(fd);
55 }
緙栬瘧錛?br>
1 gcc -o server server.c
2 gcc -o client client.c
榪愯:
1 # server绔?br>2 ./server
3 # client绔?br>4 ./client 127.0.0.1


Hideto 2008-08-06 14:59 鍙戣〃璇勮
]]>
欧美一区二区三区久久综| 美女写真久久影院| 精品国产乱码久久久久软件| 久久久无码精品亚洲日韩京东传媒 | 91久久精品无码一区二区毛片| 久久国产免费| 久久人人妻人人爽人人爽| 国产99久久久国产精免费| 久久精品无码一区二区WWW| 国产成人久久精品二区三区| 久久综合亚洲色HEZYO社区| 久久96国产精品久久久| 久久久久青草线蕉综合超碰| 99久久精品国产一区二区| 亚洲va久久久噜噜噜久久狠狠| 品成人欧美大片久久国产欧美| 精品综合久久久久久888蜜芽| 亚洲天堂久久久| 久久精品无码av| 亚洲国产成人久久精品动漫| 久久久av波多野一区二区| 一极黄色视频久久网站| 久久久久亚洲AV成人网| 久久综合欧美成人| 精品熟女少妇av免费久久| 久久亚洲精精品中文字幕| 亚洲va久久久噜噜噜久久男同 | 久久被窝电影亚洲爽爽爽| 亚洲国产精品无码久久一线| 久久久久久伊人高潮影院| 国产精品成人久久久| 国产精品久久久久久久久软件 | 久久99精品久久久久久| 久久综合九色综合网站| 久久精品人成免费| 久久亚洲精品成人av无码网站 | 94久久国产乱子伦精品免费| 久久精品这里热有精品| 久久精品国产精品国产精品污| 97久久精品人妻人人搡人人玩| 国产精品女同久久久久电影院|