Linux中客户端向服务端读取时间的简单实现
客户端的连接的主要步骤有初始化套接字(socket),连接服务端(connect),接收/发送(send/recv),关闭(close)等。
服务端的连接的主要步骤有初始化套接字(socket),邦定(bind),监听(listen),接收/发送(send/recv),关闭(close)等。
由于在Linux中所有设备都可以看作文件,接收/发送(send/recv)也可以用write/read来代替。
下面是服务端service.c的代码:
#include <stdio.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <netdb.h>
#include <unistd.h>
#include <string.h>
#include <time.h>
#define HOSTNAMELEN 40 //主机名长度
#define BUFLEN 1024
#define PORT 13000 //端口
int main(int argc, char *argv[])
{
int rc;
int s1;
int sockfd;
char buf[BUFLEN+1];
char* pc;
struct sockaddr_in local;
struct hostent* hen;
//主机
hen = gethostbyname("127.0.0.1");
if (!hen)
{
perror("couldn't resolve host name");
exit(1);
}
//初始化
memset(&local, 0, sizeof(local));
local.sin_family = AF_INET;
local.sin_port = htons(PORT);
memcpy(&local.sin_addr.s_addr, hen->h_addr_list[0], hen->h_length);
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0)
{
perror("socket() failed");
exit(1);
}
//邦定
rc = bind(sockfd,(struct sockaddr *)&local,sizeof(local));
if (rc<0)
{
perror("bind() failed");
exit (1);
}
//监听
rc=listen(sockfd,5);
if (rc)
{
perror("listen() failed");
exit(1);
}
//接收
s1=accept(sockfd,NULL,NULL);
if(s1<0)
{
perror("accpet() failed");
exit(1);
}
//取得时间
time_t ti;
ti=time(NULL);
char str[100];
sprintf(str,ct
相关文档:
1. HCI层协议概述:
HCI提供一套统一的方法来访问Bluetooth底层。如图所示:
从图上可以看出,Host Controller Interface(HCI) 就是用来沟通Host和Module。Host通常就是PC, Module则是以各种物理连接形式(USB,serial,pc-card等)连接到PC上的bluetooth Dongle。
在Host这一端:application,SDP,L2cap等协议 ......
Service Discovery Protocol(SDP)提供一种能力,让应用程序有方法发现哪种服务可用以及这种服务的特性。
服务发现协议(SDP或Bluetooth SDP)在蓝牙协议栈中对蓝牙环境中的应用程序有特殊的含意,发现哪个服务是可用的和确定这些可用服务的特征。SDP定义了bluetooth client发现可用bluetooth server服务和它们的特征的方法。 ......
22.1.1 Linux启动的基本步骤
要完整讲述Linux的启动过程,需要追溯到按下电源开关的那一刻。PC引导的第一步是执行存储在ROM(只读存储器)中代码,这种引导代码通常被称为BIOS(基本输入输出系统,Basic Input/Ouput System)。BIOS知道和引导有关的硬件设备的信息,包括磁盘、键盘、串行口、并行口等,并根据设置选 ......
这有几篇文章,值得一读
http://blog.csdn.net/wuxiaoming1733/archive/2008/10/29/3175296.aspx
http://blog.csdn.net/wooin/archive/2006/12/30/1468797.aspx
http://blog.csdn.net/wooin/archive/2006/03/10/620791.aspx
http://blog.csdn.net/wuxiaoming1733/archive/2008/10/13/3068226.aspx
http://blog.csdn.ne ......
以下以Fedora12发行版为例。
基本环境:
Fedora的完整安装盘已经包括了Qt开发环境,安装时注意选择安装Eclipse、C/C++开发和Qt开发的组件即可。如果是想添加功能,可以从Fedora的“添加/删除软件”程序里自行从软件库下载相关软件。
Qt Eclipse Integration for C++的安装:
参考:http://qt.nokia.com/devel ......