I was just beginning to touch the Linux socket programming, while the edge of the understanding of UDP socket programming, my question is that the server does not specify the IP address, the client's destination IP address is 127.0.0.1, so you can communicate? Is it not possible to test under the same host? If the server defaults to the IP address of the machine, then when server is turned on, enter 127.0.0.1: (corresponding port) in the browser to get the data? My program is as follows, did not run up, in fact I think I will not test, do not know the nature so there will be details missing. (The system used is Ubuntu 11.10, compiler GCC, respectively, running the generated. Out file in two terminals under the same host.) Thank you in advance for your answer, no matter how much, your answer makes a beginner very warm ~ ~
Server programs
?
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657 |
#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;
}
|
Client program:
?
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253 |
#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;
}
|
Socket communication programming for Linux system UDP 3