首页 > 代码库 > linux系统UDP的socket通信编程3

linux系统UDP的socket通信编程3

我刚开始接触linux下的socket编程,边抄边理解udp socket编程,我的疑问是server不指定IP地址,client的目标IP地址是127.0.0.1,这样就可以通信吗?在同一主机下是不是不能测试?如果server默认绑定本机的IP 地址,那么开启server后,在浏览器中输入127.0.0.1:(相应端口) 也可以得到数据?我的程序如下,没有跑起来,其实我觉得我不会测试,不知道本质所以会有细节漏掉。(使用的系统是ubuntu 11.10 ,编译器GCC ,分别在同一主机下的两个终端中运行生成的.out文件。)提前感谢大家的回答,不管多少,您的回答让一个初学者非常暖心~~

服务器程序

 

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include<stdio.h>
#include<netinet/in.h>
#include<arpa/inet.h>
#include<unistd.h>
#include<sys/types.h>
#include<sys/socket.h>
 
#define lport 4567
 
int main()
{
    int mysock,len;
    struct sockaddr_in addr;
    int i=0;
    char msg[256];
 
    int addr_len;
 
    if((mysock=socket(AF_INET,SOCK_DGRAM,0))<0)
    {
        perror("error");
        exit(1);
    }
    else
    {
        printf("socket created.\n");
        printf("socket ID: %d\n",mysock);
    }
    addr_len=sizeof(struct sockaddr_in);
    bzero(&addr,sizeof(addr));
    addr.sin_family=AF_INET;
    addr.sin_port=htons(lport);
    addr.sin_addr.s_addr=htonl(INADDR_ANY);
 
    if(bind(mysock,&addr,sizeof(addr))<0)
    {
        perror("connect");
        exit(1);
    }
    else
    {
        printf("bind ok.\n");
        printf("local port: %d\n",lport);
    }
    while(1)
    {
        bzero(msg,sizeof(msg));
        len=recvfrom (mysock,msg,sizeof(msg),0,&addr,&addr_len);
 
        printf("%d: ",i);
        i++;
        printf("message form: %s\n",inet_ntoa(addr.sin_addr));
        printf("message: %s\n\n",msg);
        sendto(mysock,msg,len,0,&addr,addr_len);
    }
    return 0;
}

 

客户机程序:

 

 

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include<stdio.h>
#include<netinet/in.h>
#include<arpa/inet.h>
#include<unistd.h>
#include<fcntl.h>
#include<sys/stat.h>
#include<sys/types.h>
#include<sys/socket.h>
 
#define rport 4567
#define rip "127.0.0.1"
 
int main()
{
    int s,len;
    struct sockaddr_in addr;
    int addr_len;
    char msg[256];
    int i=0;
 
    if((s=socket(AF_INET,SOCK_DGRAM,0))<0)
    {
        perror("error");
        exit(1);
    }
    else
    {
        printf("socket created.\n");
        printf("socket id: %d\n",s);
        printf("romote ip: %s\n",rip);
        printf("remote port: %d\n",rport);
    }
 
    len=sizeof(struct sockaddr_in);
    bzero(&addr,sizeof(addr));
    addr.sin_family=AF_INET;
    addr.sin_port=htons(rport);
    addr.sin_addr.s_addr=inet_addr(rip);
 
    while(1)
    {
        bzero(msg,sizeof(msg));
        len=read(STDIN_FILENO,msg,sizeof(msg));
        sendto(s,msg,len,0,&addr,addr_len);
        printf("\nInput message: %s\n",msg);
        len=recvfrom(s,msg,sizeof(msg),0,&addr,&addr_len);
 
        printf("%d :",i);
        i++;
        printf("received message: %s \n",msg);
    }
    return 0;
}

linux系统UDP的socket通信编程3