linux socket编程基于本地unix域格式的协议族
头文件:
#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;
}
客户端代码:
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,
相关文档:
今天,把windows下的工程导入到了Linux下eclipse中,由于以前的工程代码,都是GBK编码的,而Ubuntu默认是不支持GBK编码的。所以,首先我们要先让Ubuntu支持GBK,方法如下:
1.
修改/var/lib/locales /supported.d/local文件,在文件中添加
z ......
原文地址:http://soft.yesky.com/os/lin/326/2237826.shtml
本文详细地介绍了在 Linux 2.6.13 内核中新引入的文件系统变化通知机制 inotify,并举例说明了它的使用与典型应用案例。
一、引言
众所周知,Linux 桌面系统与 MAC 或 Windows 相比有许多不如人意的地方,为了改善这种状况,开源社区提出用户态需要内核提供 ......
1.gcc包含的c/c++编译器
gcc,cc与c++,g++
gcc和cc是一样的,c++和g++是一样的。一般c程序就用gcc编译,c++程序就用g++编译。
2.gcc的基本用法
gcc test.c:如果没有指定输出的文件,默认将编译出一个名为a.out的程序
gcc test.c -o test:-o参数用来指定生成目标程序的名字,这样将编译出一个名为test的程序。
3.为什 ......
<!--
@page { margin: 2cm }
P { margin-bottom: 0.21cm }
-->
此前,中国的
Linux“
产业”并未与国际
Linux
运动正式接轨,自行其是。但是,从此以后,情况就不同了。为什么?
5
月
10
日,国际
Linu ......
LINUX网卡绑定聚合设置
1.建立虚拟网络接口配置文件:ifcfg-bond0
cd /etc/sysconfig/ network-scripts/
[root@rhas-13 root]# vi ifcfg-bond0 (建立ifcfg-bond0文件)
2.编辑虚拟网络接口配置文件
#vi ifcfg-bond0
将第一行改成 DEVICE=bond0
# cat ifcfg-bond ......