/* receiver */
#include <sys/types.h>
#include <sys/socket.h>
struct sockaddr myname;
struct sockaddr from_name;
char buf[80];
main()
{
int sock;
int fromlen, cnt;
sock = socket(AF_UNIX, SOCK_DGRAM, 0);
if (sock < 0) {
printf("socket failure %d\n", errno);
exit(1);
}
myname.sa_family = AF_UNIX;
strcpy(myname.sa_data, "/tmp/tsck");
if (bind(sock, &myname, strlen(myname.sa_data) +
sizeof(name.sa_family)) < 0) {
printf("bind failure %d\n", errno);
exit(1);
}
cnt = recv(sock, buf, sizeof(buf)); // recfrom not work !!
if (cnt < 0) {
printf("recvfrom failure %d\n", errno);
exit(1);
}
buf[cnt] = '\0'; /* assure null byte */
from_name.sa_data[fromlen] = '\0';
printf("'%s' received from %s\n",
buf, from_name.sa_data);
}
/* sender */
#include <sys/types.h>
#include <sys/socket.h>
char buf[80];
struct sockaddr to_name;
main()
{
int sock;
sock = socket(AF_UNIX, SOCK_DGRAM, 0);
if (sock < 0) {
printf("socket failure %d\n", errno);
exit(1);
}
to_name.sa_family = AF_UNIX;
strcpy(to_name.sa_data, "/tmp/tsck");
strcpy(buf, "test data line");
cnt = sendto(sock, buf, strlen(buf), 0, &to_name,
strlen(to_name.sa_data) + sizeof(to_name.sa_family));
if (cnt < 0) {
printf("sendto failure %d\n", errno);
exit(1);
}
}
我修改了一下 android 的 test_udp (system/extras/tests/bionic/libc/common/test_udp.c)
#define PROGNAME "test_udp"
#define DEFAULT_PORT 7000
#include <arpa/inet.h>
#include <netinet/in.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <unistd.h>
#include <string.h>
#define BUFLEN 512
#define NPACK 10
void diep(char *s)
{
perror(s);
exit(1);
}
static void
usage(int code)
{
printf("usage: %s [options]\n", PROGNAME);
printf("options:\n");
printf(" -p<port> use specific port (default %d)\n", DEFAULT_PORT);
printf(" -a<inet> use specific IP address\n");
printf(" -s run server (default is client)\n");
exit(code);
}
int main(int argc, char** argv)
{
int runServer = 0;
int udpPort = DEFAULT_PORT;
int useLocal = 0;
int address = htonl(INADDR_ANY);
struct sockaddr si_me, si_other;
int s, i, slen;
char buf[BUFLEN];
int cnt;
while (argc > 1 && argv[1][0] == '-') {
const char* optName = argv[1]+1;
argc--;
argv++;
switch (optName[0]) {
case 's':
runServer = 1;
break;
default:
usage(1);
}
}
if (runServer) {
if ((s=socket(AF_UNIX, SOCK_DGRAM, 0))==-1)
diep("socket");
si_me.sa_family = AF_UNIX;
strcpy(si_me.sa_data,"/data/tsck");
if (bind(s, &si_me, strlen(si_me.sa_data)+sizeof(si_me.sa_family))==-1)
diep("bind");
printf("UDP server listening \n");
for (i=0; i<NPACK; i++) {
cnt = read(s, buf, BUFLEN);
printf("cnt=%d\n",cnt);
if(cnt>=0){
buf[cnt]='\0';
printf("Received packet Data: %s\n",buf);
}
}
printf("UDP server closing\n");
close(s);
unlink("/data/tsck");
}
else /* !runServer */
{
if ((s=socket(AF_UNIX, SOCK_DGRAM, 0))==-1)
diep("socket");
si_other.sa_family = AF_UNIX;
strcpy(si_other.sa_data,"/data/tsck");
printf("UDP client sending packets \n");
for (i=0; i<NPACK; i++) {
printf("Sending packet %d\n", i);
sprintf(buf, "This is packet %d\n", i);
if (sendto(s, buf, BUFLEN, 0, &si_other,
strlen(si_other.sa_data)+sizeof(si_other.sa_family))==-1)
diep("sendto()");
}
close(s);
printf("UDP client closing\n");
}
return 0;
}
server close 後要 unlink,否則會留一個 file 在 /data/tsck。下次再 bind 就會 fail
ref: AF_UNIX, SOCK_DGRAM 好像不能用 recvfrom, 只能用 read, 否則有 data 近來時會有 argument error.
沒有留言:
張貼留言