linux Socket±à³ÌÈëÃÅ
ÒÔÇ°ÒѾ½Ó´¥WindowsϵÄSocket±à³Ì£¬×î½üÒ²³¢ÊÔ×ÅÔÙlinuxÏÂʹÓÃSocket±à³Ì£¬ÏÂÃæÊÇÔÚÍøÉÏÕÒµÄÀý×Ó(ÒѾ²âÊÔ¹ý)
tcp_server.c:
#include <stdio.h>
#include <sys/socket.h>
#include <unistd.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <stdlib.h>
#include <time.h>
#define SERVER_PORT 20000
#define LENGTH_OF_LISTEN_QUEUE 10
#define BUFFER_SIZE 255
#define WELCOME_MESSAGE "welcome to connect the server"
int main(int argc, char **argv)
{
int servfd,clifd;
struct sockaddr_in servaddr,cliaddr;
if((servfd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
{
printf("create socket error!\n");
exit(1);
}
bzero(&servaddr, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_port = htons(SERVER_PORT);
servaddr.sin_addr.s_addr = htons(INADDR_ANY);
if(bind(servfd,(struct sockaddr*)&servaddr,sizeof(servaddr)) < 0)
{
printf ("bind to port %d failure!\n",SERVER_PORT);
exit(1);
}
if(listen(servfd, LENGTH_OF_LISTEN_QUEUE) < 0)
{
printf("call listen failure!\n");
exit(1);
}
while(1)
{
char buf[BUFFER_SIZE];
long timestamp;
socklen_t length = sizeof(cliaddr);
clifd = accept(servfd, (struct sockaddr*)&cliaddr, &length);
if(clifd < 0)
{
printf("error comes when call accept!\n");
break;
}
strcpy(buf,WELCOME_MESSAGE);
printf("from client, IP:%s, Port:%d\n", inet_ntoa(cliaddr.sin_addr), ntohs(cliaddr.sin_port));
timestamp = time(NULL);
strcat(buf, "timestamp in server:");
strcat(buf, ctime(×tamp));
send(clifd, buf, BUFFER_SIZE, 0);
close(clifd);
}
close(servfd);
return 0;
}
tcp_clie
Ïà¹ØÎĵµ£º
ShellÊÇϵͳµÄÓû§½çÃ棬ÌṩÁËÓû§ÓëÄں˽øÐн»»¥²Ù×÷µÄÒ»ÖÖ½Ó¿Ú¡£Ëü½ÓÊÕÓû§ÊäÈëµÄÃüÁî²¢°ÑËüËÍÈëÄÚºËÈ¥Ö´ÐС£
¡¡¡¡Êµ¼ÊÉÏShellÊÇÒ»¸öÃüÁî½âÊÍÆ÷£¬Ëü½âÊÍÓÉÓû§ÊäÈëµÄÃüÁî²¢ÇÒ°ÑËüÃÇË͵½Äںˡ£²»½öÈç´Ë£¬ShellÓÐ×Ô¼ºµÄ±à³ÌÓïÑÔÓÃÓÚ¶ÔÃüÁîµÄ±à¼£¬ËüÔÊÐíÓû§±àдÓÉshellÃüÁî×é³ÉµÄ³ÌÐò¡£Shell±à³ÌÓïÑÔ¾ßÓÐÆÕͨ±à³ÌÓ ......
¡¡¡¡ÄãÏë³ÉÖªµÀÈçºÎ³ÉΪһ¸öLinuxÄں˿ª·¢Õßô£¿»òÕßÄãµÄÀÏ°å¸æËßÄ㣬“ȥΪÕâ¸öÉ豸дһ¸öLinuxÇý¶¯¡£“ÕâƪÎĵµµÄÄ¿µÄ£¬¾ÍÊÇͨ¹ýÃèÊöÄãÐèÒª¾ÀúµÄ¹ý³ÌºÍÌáʾÄãÈçºÎºÍÉçÇøÒ»Æð¹¤×÷£¬À´½Ì¸øÄãΪ´ïµ½ÕâЩĿµÄËùÐèÒªÖªµÀµÄËùÓÐ֪ʶ¡£±¾ÎÄÒ²³¢ÊÔ½âÊÍÉçÇøΪʲôÕâÑù¹¤×÷µÄһЩÔÒò¡£
¡¡¡¡Äں˼¸ºõÈ«ÊÇÓÃCд³ÉµÄ£¬ÓÐ ......
ÕâÊdzöϯÔÚ2009Äê10ÔÂ20ÈÕÔÚ¶«¾©¾ÙÐеÄLinuxÄں˴ó»áºÏÓ°ÕÕ¡£È«·Ö±æÂÊÕÕƬ¡£Ò»¸öÅ®ÐÔ¶¼Ã»ÓС£ÄãÈÏʶÆäÖеļ¸Î»ÄØ£¿
ÒÔÏÂÊdzöϯ»áÒéµÄÃûµ¥£¬ÓÐÐËȤµÄÅóÓÑ¿ÉÒÔ¶ÔÒ»¶Ô£º
Al Viro
Alan Cox
Andi Kleen
Andrew Morton
Andy Whitcroft
Arjan van de Ven
Arnd Bergmann
Avi Kivity
Benjamin Herrenschmidt
Brian ......
#include <stdio.h>
#include <unistd.h>
#define FOO "foo"
int main(void)
{
if(!access(FOO, F_OK))
{
if(!unlink(FOO))
{
}
else
{
printf("remove %s failed\n", FOO);
}
}
else
{
printf("%s not existed\ ......
/*
kmalloc can apply 128KB memory only. This func support any continous memory allocate more than 2MB.
*/
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/module.h>
#include <linux/kallsyms.h>
#define KMEM_PAGES &nb ......