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ĬÈÏÊDz»Ö§³ÖGBK±àÂëµÄ¡£ËùÒÔ£¬Ê×ÏÈÎÒÃÇÒªÏÈÈÃUbuntuÖ§³ÖGBK£¬·½·¨ÈçÏ£º
1.
ÐÞ¸Ä/var/lib/locales /supported.d/localÎļþ,ÔÚÎļþÖÐÌí¼Ó
z ......
¶Ô±È£ºLinuxºÍWindowsϵͳÏ̼߳äµÄÇø±ð
ÊìϤWIN32±à³ÌµÄÈËÒ»¶¨ÖªµÀ£¬WIN32µÄ½ø³Ì¹ÜÀí·½Ê½ÓëLinuxÉÏÓÐןܴóÇø±ð£¬ÔÚUNIXÀֻÓнø³ÌµÄ¸ÅÄµ«ÔÚWIN32ÀïÈ´»¹ÓÐÒ»¸ö"Ïß³Ì"µÄ¸ÅÄÄÇôLinuxºÍWIN32ÔÚÕâÀ﾿¾¹ÓÐ×ÅÊ²Ã´Çø±ðÄØ£¿
WIN32ÀïµÄ½ø³Ì/Ïß³ÌÊǼ̳Ð×ÔOS/2µÄ¡£ÔÚWIN32À"½ø³Ì"ÊÇÖ¸Ò»¸ö³ÌÐò£¬¶ø"Ïß³Ì"ÊÇÒ ......
ÔÚ³£¹æIOÖУ¬ÎÒÃÇÒ»°ã¶¼ÊÇÈÏΪºÍÎļþϵͳ×öIO²Ù×÷×ÜÊÇÎÞ·¨±ÜÃâµÄ±ØÐë²»ÑáÆä·³µÄwrite£¬read£¬¶øÊÂʵÉÏÕâÖÖ·½Ê½ÔÚ½øÐÐһЩ´óÎļþ²Ù×÷µÄʱºò£¬ÏÔµÃÊ®·ÖµÄ²»±ã£¬ÏÂÃæÕâ¶Î´úÂ뼴չʾÁËÈç¹û½«ÎïÀíÓ²ÅÌÉϵÄÊý¾ÝÓ³Éäµ½ÄÚ´æÖУ¬ÎÒÃÇÏñ²Ù×÷ÆÕͨµÄÄÚ´æÒ»Ñù¶ÔÎļþ½øÐжÁд£º
#include <sys/mman.h>
#include <sys/types.h&g ......
ʲôʱºòÐèÒª´´½¨Ï̳߳ØÄØ£¿¼òµ¥µÄ˵£¬Èç¹ûÒ»¸öÓ¦ÓÃÐèҪƵ·±µÄ´´½¨ºÍÏú»ÙỊ̈߳¬¶øÈÎÎñÖ´ÐеÄʱ¼äÓַdz£¶Ì£¬ÕâÑùÏ̴߳´½¨ºÍÏú»ÙµÄ´øÀ´µÄ¿ªÏú¾Í²»ÈݺöÊÓ£¬ÕâʱҲÊÇÏ̳߳ظóö³¡µÄ»ú»áÁË¡£Èç¹ûÏ̴߳´½¨ºÍÏú»Ùʱ¼äÏà±ÈÈÎÎñÖ´ÐÐʱ¼ä¿ÉÒÔºöÂÔ²»¼Æ£¬ÔòûÓбØÒªÊ¹ÓÃÏ̳߳ØÁË¡£ÏÂÃæÊÇLinuxϵͳÏÂÓÃCÓïÑÔ´´½¨µÄÒ»¸öÏ̳߳ء£Ïß³Ì³Ø ......