Hideto

  C++博客 :: 首页 :: 联系 :: 聚合  :: 管理
  2 Posts :: 0 Stories :: 0 Comments :: 0 Trackbacks

常用链接

留言簿(17)

我参与的团队

搜索

  •  

最新评论

阅读排行榜

评论排行榜

2008年8月8日 #

server.erl:
 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!"
posted @ 2008-08-08 12:40 Hideto 阅读(666) | 评论 (0)编辑 收藏

2008年8月6日 #

server.c:
 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 }
编译:
1 gcc -o server server.c
2 gcc -o client client.c
运行:
1 # server端
2 ./server
3 # client端
4 ./client 127.0.0.1
posted @ 2008-08-06 14:59 Hideto 阅读(351) | 评论 (0)编辑 收藏