linux socket编程基于本地unix域格式的协议族
2010-05-11 10:16
465 查看
头文件:
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <sys/select.h>
服务器端代码:
int main(int argc, char **argv)
{
struct sockaddr_un address;
int sock, conn;
int addrLength;
char buf[1024] = {0};
char *msg = "I has recived";
if ((sock = socket(AF_UNIX, SOCK_STREAM, 0)) < 0)
{
perror("socket");
exit(0);
}
unlink("foo.sock");
address.sun_family = AF_UNIX;
strcpy(address.sun_path, "foo.sock");
addrLength = sizeof(address.sun_family) + strlen(address.sun_path);
if(bind(sock, (struct sockaddr *) &address, addrLength))
perror("bind");
if(listen(sock, 5))
perror("listen");
while((conn = accept(sock, (struct sockaddr *) &address, &addrLength)) >= 0)
{
recv(conn, buf, sizeof(buf), 0);
printf("%s/n", buf);
send(conn, msg, strlen(msg), 0);
}
return 0;
}
客户端代码:
#define SOCK_PATH "foo.sock"
int main(int argc, char **argv)
{
int client_fd;
int len;
ssize_t I;
char *msg = "hello server";
char buf[1024] = {0};
struct sockaddr_un remote;
if((client_fd = socket(AF_LOCAL, SOCK_STREAM, 0)) == -1)
{
perror("socket()");
exit(0);
}
remote.sun_family = AF_LOCAL;
strcpy(remote.sun_path, SOCK_PATH);
len = sizeof(remote);
//puts("hello, hello, hello, hello");
if(connect(client_fd, (struct sockaddr *)&remote, len) == -1)
{
strerror(errno);
exit(0);
}
//puts("world, world, world");
if(send(client_fd, msg, strlen(msg), 0) == -1)
{
strerror(errno);
exit(0);
}
puts("hhhhhhh");
if(recv(client_fd, buf, sizeof(buf), 0) == -1)
{
perror("recv:");
exit(0);
}
printf("%s/n", buf);
}
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <sys/select.h>
服务器端代码:
int main(int argc, char **argv)
{
struct sockaddr_un address;
int sock, conn;
int addrLength;
char buf[1024] = {0};
char *msg = "I has recived";
if ((sock = socket(AF_UNIX, SOCK_STREAM, 0)) < 0)
{
perror("socket");
exit(0);
}
unlink("foo.sock");
address.sun_family = AF_UNIX;
strcpy(address.sun_path, "foo.sock");
addrLength = sizeof(address.sun_family) + strlen(address.sun_path);
if(bind(sock, (struct sockaddr *) &address, addrLength))
perror("bind");
if(listen(sock, 5))
perror("listen");
while((conn = accept(sock, (struct sockaddr *) &address, &addrLength)) >= 0)
{
recv(conn, buf, sizeof(buf), 0);
printf("%s/n", buf);
send(conn, msg, strlen(msg), 0);
}
return 0;
}
客户端代码:
#define SOCK_PATH "foo.sock"
int main(int argc, char **argv)
{
int client_fd;
int len;
ssize_t I;
char *msg = "hello server";
char buf[1024] = {0};
struct sockaddr_un remote;
if((client_fd = socket(AF_LOCAL, SOCK_STREAM, 0)) == -1)
{
perror("socket()");
exit(0);
}
remote.sun_family = AF_LOCAL;
strcpy(remote.sun_path, SOCK_PATH);
len = sizeof(remote);
//puts("hello, hello, hello, hello");
if(connect(client_fd, (struct sockaddr *)&remote, len) == -1)
{
strerror(errno);
exit(0);
}
//puts("world, world, world");
if(send(client_fd, msg, strlen(msg), 0) == -1)
{
strerror(errno);
exit(0);
}
puts("hhhhhhh");
if(recv(client_fd, buf, sizeof(buf), 0) == -1)
{
perror("recv:");
exit(0);
}
printf("%s/n", buf);
}
相关文章推荐
- linux网络编程之socket(十四):基于UDP协议的网络程序
- 网络编程(5)—— 基于Linux系统的UDP协议socket服务器和客户端
- linux网络编程之socket(十四):基于UDP协议的网络程序
- Linux下的socket编程实践(六)Unix域协议和socketpair传递文件描述符
- linux网络编程之socket(十四):基于UDP协议的网络程序
- linux网络编程之socket(十四):基于UDP协议的网络程序
- Linux网络编程之[基于socket通信的tcp协议的编程模型]
- Linux Socket网络编程 分别使用TCP协议 和 UDP协议
- Linux下基于C/C++的Socket编程实例
- 基于UDP协议之——socket编程
- Linux下基于C/C++的Socket编程基础
- Socket编程实践(13) --UNIX域名协议
- socket编程 -- 基于UDP协议的C/S通信模型及实现
- 14.基于UDP协议的socket编程(1)
- LINUX下Socket编程 函数格式详解
- 好好学习,天天向上【C/C++/STL】【 多线程/Socket编程】【MySQL/Oracle】【Linux/Unix】
- Linux/Unix下Socket编程
- 网络协议(四)Socket编程之Unix
- 基于Linux的socket编程模板
- Linux编程之socket:tcp流协议产生的粘包问题及解决方法