linux下tcp服务器源码示例
#include <errno.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdlib.h>
#include <string.h>
#include <netinet/in.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
struct _NSS_HEADER
{
unsigned short ProtocolVersion; /* 协议版本信息 */
unsigned short MsgType; /* 消息类型 */
unsigned short TransactionNo; /* 传输编号 */
unsigned short PacketLength; /* 数据包长度,包括Header */
}NSS_HEADER;
void str_echo(int sockfd)
{
ssize_t readLen;
ssize_t writeLen;
char buf[8];
while ( true )
{
readLen = read(sockfd, buf, 8);
if (readLen < 0 && errno == EINTR)
{
continue;
}
else if ( readLen <= 0 )
{
perror( "read:");
return ;
}
printf( "recv data successed. data len: %d\n", readLen );
int nWriteLen = 0;
while ( true )
{
writeLen == write(sockfd, &buf[nWriteLen], readLen-nWriteLen);
if (writeLen < 0 && errno == EINTR)
{
continue;
}
else if ( writeLen < 0 )
{
perror ( "write:" );
return;
}
nWriteLen += writeLen;
// 已写完,直接返回
if (nWriteLen >= readLen )
{
break;
}
}
printf( "send data successed. data len: %d\n", readLen );
}
}
int main(int argc, char **argv)
{
printf( "server ip: %s\n", argv[1] );
printf( "server port: %s\n", argv[2] );
printf( "\nservice starting ... \n\n" );
int listenfd, connfd;
pid_t childpid;
socklen_t clilen;
struct sockadd
相关文档:
引自:http://server.it168.com/a2009/0309/267/000000267930.shtml
4.3 Linux进程管理命令详解(1)
Linux管理进程的最好方法就是使用命令行下的系统命令。Linux下面的进程涉及的命令有at, bg, fg, kill, crontab, jobs, ps, pstree, top, nice, renice, sleep, nohup。
1.at命令:定时运行命令
作用: ......
引自:http://server.it168.com/a2009/0309/267/000000267918.shtml
4.1.7 Linux的第一个进程:init(1)
init是Linux系统执行的第一个进程,进程ID为1,是系统所有进程的起点,主要用来执行一些开机初始化脚本和监视进程。Linux系统在完成内核引导以后就开始运行init程序,init程序需要读取配置文件/etc/inittab。init ......
文章选取的例子非常简单,上手容易,只是为了讲述静态与动态链接库的生成和链接过
程,还有他们之间的区别。以下例子在 gcc 4.1.1 下顺利通过。
文件预览
文件目录树如下,如你所见,非常简单。
libtest/
|-- lt.c
|-- lt.h
`-- test. ......
由操作系统实现的所有系统调用所构成的集合即程序接口或应用编程接口(Application Programming Interface,API)。是应用程序同系统之间的接口。
操作系统的主要功能是为应用程序的运行创建良好的环境,为了达到这个目的,内核提供一系列具备预定功能的的内核函数,通过一组称为系统调用的(system call)的接口呈现给用 ......
linux shell pwd 显示当前路径
假若有test.cpp
g++ test.cpp -o test
./test
想在test中找到当前执行程序所在的路径
可以再test.cpp中使用readlink函数
具体见如下实例:
#include<iostream>
#include<unistd.h>
#include<dirent.h>
#include<string.h>
#include<string>
using ......