http://www.hackerschool.org/HS_Boards/zboard.php?id=Free_Lectures&no=1627 [º¹»ç]
¼¹öÄÚµå
#include <stdio.h>
#include <netinet/in.h>
#include <sys/socket.h>
#define PORT 9000
#define BSIZE 30
char buffer[BSIZE]="Hello,World!\n";
char rBuffer[BSIZE];
int main(){
int len,n,length;
int c_sock,s_sock;
char *temp;
struct sockaddr_in s_addr,c_addr;
s_sock=socket(PF_INET,SOCK_STREAM,0);
memset(&s_addr,0,sizeof(s_addr));
s_addr.sin_addr.s_addr=htonl(INADDR_ANY);
s_addr.sin_family=AF_INET;
s_addr.sin_port=htons(PORT);
if(bind(s_sock,(struct sockaddr*)&s_addr,sizeof(s_addr))==-1){
printf("Can Not Bind\n");
return -1;
}
if(listen(s_sock,5)==-1){
printf("Listen Fail\n");
return -1;
}
while(1){
len=sizeof(c_addr);
c_sock=accept(s_sock,(struct sockaddr*)&c_addr,&len);
length=0;
temp=rBuffer;
while((n=read(c_sock,temp,1))>0){
if(*temp=='\r') continue;
if(*temp=='\n') break;
if(*temp=='\0') break;
if(length==BSIZE) break;
temp++;
length++;
}
rBuffer[length]='\0';
printf("receive: %s\n",rBuffer);
if(!strcmp(rBuffer,"printf")){
n=strlen(buffer);
write(c_sock,buffer,n);
}
close(c_sock);
}
close(s_sock);
return 0;
}
Ŭ¶óÀ̾ðÆ® ÄÚµå
(require :asdf)
(asdf:operate 'asdf:load-op 'sb-bsd-sockets)
(defparameter @sock-s
(make-instance 'sb-bsd-sockets:inet-socket :type :stream :protocol :tcp))
(defparameter @host
(sb-bsd-sockets:host-ent-address
(sb-bsd-sockets:get-host-by-name "localhost")))
(defparameter @port 9000)
(defparameter @foo nil)
(sb-bsd-sockets:socket-connect @sock-s @host @port)
(sb-bsd-sockets:socket-send @sock-s "printf" 10)
(setf @foo(sb-bsd-sockets:socket-receive @sock-s nil 30))
(sb-bsd-sockets:socket-close @sock-s)
(format t "*result: ~a~%" @foo)
¼¹öÄÚµå´Â ¿Ç÷°ÀÇ TCP/IP ¼ÒÄÏ ÇÁ·Î±×·¡¹Ö¿¡¼ ³ª¿À´Â helloserver ÄÚµåÀÔ´Ï´Ù.
printf¶ó´Â ¸í·ÉÀ» ¹Þ¾ÒÀ» °æ¿ì, Hello,World!¶ó´Â ¹®ÀÚ¿À» Àü¼ÛÇØÁÖ´Â ÇÁ·Î±×·¥ÀÌ°í,
ÀÌ ¼¹ö¸¦ »ç¿ëÇÒ¼ö ÀÖµµ·Ï sbcl¿¡¼ ±âº»ÀûÀ¸·Î Á¦°øÇÏ´Â sb-bsd-socketsÆÐÅ°Áö¸¦ »ç¿ëÇÏ´Â ¹æ¹ýÀ» Å×½ºÆ®ÇغýÀ´Ï´Ù.
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
host-ent-address[sb-bsd-sockets] host-end-point
ÁöÁ¤µÈ È£½ºÆ®¿¡ ´ëÇÑ Á¢±Ù°ü·Ã Á¤º¸¸¦ ¹ÝȯÇÕ´Ï´Ù.
EndPoint¸¦ »ý¼ºÇϴµ¥¿¡´Â ¾ÆÀÌÇÇ ¶Ç´Â ÇØ´ç ¼¹ö¸¦ ³ªÅ¸³¾¼ö ÀÖ´Â ¹®ÀÚ¿À» EndPointÇü °´Ã¼·Î º¯È¯ÇÏ´Â ÇÔ¼ö
get-host-by-nameÀÌ »ç¿ëµË´Ï´Ù.
(make-instance 'sb-bsd-sockets:inet-socket :type :stream :protocol :tcp))
¼ÒÄÏÀ» »ç¿ëÇϱâ À§Çؼ´Â sb-bsd-socketsÆÐÅ°Áö ³»ºÎ¿¡ Á¤ÀǵǾî ÀÖ´Â inet-socket Ŭ·¡½º¸¦ ÀνºÅϽº ÇÒ ÇÊ¿ä°¡ ÀÖ½À´Ï´Ù.
À§ Äڵ忡¼´Â tcp ¼ÒÄÏ ÀνºÅϽº¸¦ »ý¼ºÇÏ°í ÀÖ½À´Ï´Ù.
socket-connect[sb-bsd-sockets] socket host port
À§¿¡¼ »ý¼ºµÈ ¼ÒÄÏ, È£½ºÆ®Á¤º¸,Æ÷Æ®¹øÈ£·Î ½ÇÁ¦·Î ¼¹ö¿¡ Á¢¼ÓÇÏ´Â µ¿ÀÛÀ» ¼öÇàÇÕ´Ï´Ù.
socket-send[sb-bsd-sockets] socket buffer length
bufferÀÇ ³»¿ëÀ» ¼ÒÄÏÀ» ÅëÇØ Àü¼ÛÇÕ´Ï´Ù.
sock-receive[sb-bsd-sockets] socket buffer length
socketÀ» ÅëÇØ Àü¼ÛµÇ¾î¿Â µ¥ÀÌÅ͸¦ ¹ÝȯÇÕ´Ï´Ù.
ÀÌ ÇÔ¼ö¿¡¼´Â buffer ¶Ç´Â lengthÁß Çϳª¸¸ ÁöÁ¤ÇØ ÁÖ¸é µÇ´Âµ¥,
±× ÀÌÀ¯´Â length°¡ ÁöÁ¤µÉ °æ¿ì buffer ÁöÁ¤Àº ¹«½ÃµÇ°í, ÁöÁ¤µÈ Å©±â ¸¸ÅÀÇ »õ·Î¿î ¹öÆÛ¸¦ ¸¸µé¾î ÇÔ¼ö Á¾·áÈÄ
¼ö½Å °á°ú¸¦ ¹Ýȯ°ªÀ¸·Î¼ ´øÁ®ÁÖ°Ô µË´Ï´Ù.
¸¸¾à, buffer¸¦ ÁöÁ¤ÇØÁÖ°í length¸¦ ÁöÁ¤ÇØ ÁÖÁö ¾Ê¾ÒÀ» °æ¿ì¿¡´Â buffer°¡ Â÷ÁöÇÏ°í ÀÖ´Â ¿µ¿ªÀ§¿¡ ¼ö½Å µ¥ÀÌÅ͸¦
µ¤¾î¾²°Ô µË´Ï´Ù.
´ëºÎºÐ ÀÌ·¯ÇÑ Ã³¸®´Â ¿øÄ¡ ¾Ê±â ¶§¹®¿¡ buffer ¿µ¿ªÀ» ºñ¿öµÎ°í, length°ªÀ» ÁöÁ¤ÇÏ´Â ÇüÅ·Π»ç¿ëµË´Ï´Ù.
sock-close[sb-bsd-sockets] socket
¿·Á ÀÖ´Â ¼ÒÄÏÀ» ´Ý½À´Ï´Ù.
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
¿Ïº®ÇÑ ³×Æ®¿öÅ© ÇÁ·Î±×·¥ÀÌ µÇ±â À§Çؼ´Â Ŭ¶óÀ̾ðÆ® Äڵ忡µµ ŸÀӾƿô 󸮵îÀÌ µé¾î°¡¾ß ÇÏÁö¸¸,
ÀÌ ¿¹Á¦´Â ³×Æ®¿öÅ© Á¢¼ÓÀ» À§ÇÑ ÃÖ¼ÒÇÑÀÇ ÇÔ¼öÀÇ »ç¿ë¿¹¸¦ º¸¿©ÁÖ±â À§ÇÑ°«ÀÌ´Ï ³Ñ¾î°¡µµ·Ï ÇÏ°Ú½À´Ï´Ù. |
Hit : 6347 Date : 2010/12/27 10:36
|