을 활용한 프로그래밍에 있어서 /간의 구조를 아는것도 중요하지만
관련함수를 어떻게 사용하는지 아는것도 매우 중요한것 같다.

다음의 도표는 이를 알기 쉽게 표현한 것이다.

소켓 프로그래밍 기본 흐름 - Socket Programming Basic Flow

예제 코드를 보면 좀더 이해가 빠르지 않을까...!?

socket_server.c

#include <sys/types.h>
#include <sys/.h>
#include <sys/un.h>
#include <stdio.h>

#define NSTRS 3 /* no. of strings */
#define ADDRESS "my" /* addr to connect */

/*
* Strings we send to the .
*/
char *strs[NSTRS] = {
"This is the first string from the . ",
"This is the second string from the . ",
"This is the third string from the . "
};

main()
{
char c;
FILE *fp;
int fromlen;
register int i, s, ns, len;
struct sockaddr_un saun, fsaun;

/*
* Get a to work with. This will
* be in the UNIX domain, and will be a
* stream .
*/
if ((s = (AF_UNIX, SOCK_STREAM, 0)) < 0) {
perror(": ");
exit(1);
}

/*
* Create the address we will be binding to.
*/
saun.sun_family = AF_UNIX;
strcpy(saun.sun_path, ADDRESS);

/*
* Try to bind the address to the . We
* unlink the name first so that the bind won't
* fail.
*
* The third argument indicates the "length" of
* the structure, not just the length of the
* name.
*/
unlink(ADDRESS);
len = sizeof(saun.sun_family) + strlen(saun.sun_path);

if (bind(s, &saun, len) < 0) {
perror(": bind");
exit(1);
}

/*
* Listen on the .
*/
if (listen(s, 5) < 0) {
perror(": listen");
exit(1);
}

/*
* Accept connections. When we accept one, ns
* will be connected to the . fsaun will
* contain the address of the .
*/
if ((ns = accept(s, &fsaun, &fromlen)) < 0) {
perror(": accept");
exit(1);
}

/*
* We'll use stdio for reading the .
*/
fp = fdopen(ns, "r");

/*
* First we send some strings to the .
*/
for (i = 0; i < NSTRS; i++)
send(ns, strs[i], strlen(strs[i]), 0);

/*
* Then we read some strings from the and
* print them out.
*/
for (i = 0; i < NSTRS; i++) {
while ((c = fgetc(fp)) != EOF) {
putchar(c);

if (c == ' ')
break;
}
}

/*
* We can simply use close() to terminate the
* connection, since we're done with both sides.
*/
close(s);

exit(0);
}

socket_client.c

#include <sys/types.h>
#include <sys/.h>
#include <sys/un.h>
#include <stdio.h>

#define NSTRS 3 /* no. of strings */
#define ADDRESS "my" /* addr to connect */

/*
* Strings we send to the .
*/
char *strs[NSTRS] = {
"This is the first string from the . ",
"This is the second string from the . ",
"This is the third string from the . "
};

main()
{
char c;
FILE *fp;
register int i, s, len;
struct sockaddr_un saun;

/*
* Get a to work with. This will
* be in the UNIX domain, and will be a
* stream .
*/
if ((s = (AF_UNIX, SOCK_STREAM, 0)) < 0) {
perror(": ");
exit(1);
}

/*
* Create the address we will be connecting to.
*/
saun.sun_family = AF_UNIX;
strcpy(saun.sun_path, ADDRESS);

/*
* Try to connect to the address. For this to
* succeed, the must already have bound
* this address, and must have issued a listen()
* request.
*
* The third argument indicates the "length" of
* the structure, not just the length of the
* name.
*/
len = sizeof(saun.sun_family) + strlen(saun.sun_path);

if (connect(s, &saun, len) < 0) {
perror(": connect");
exit(1);
}

/*
* We'll use stdio for reading
* the .
*/
fp = fdopen(s, "r");

/*
* First we read some strings from the
* and print them out.
*/
for (i = 0; i < NSTRS; i++) {
while ((c = fgetc(fp)) != EOF) {
putchar(c);

if (c == ' ')
break;
}
}

/*
* Now we send some strings to the .
*/
for (i = 0; i < NSTRS; i++)
send(s, strs[i], strlen(strs[i]), 0);

/*
* We can simply use close() to terminate the
* connection, since we're done with both sides.
*/
close(s);

exit(0);
}



facebook posting twit

  • 듀얼모니터를 쓸때 태스크바가 하나 밖에 없어서 불편했던 사람들을 위한 – 듀얼모니터용 프로그램 울트라몬 (UltraMon)
  • Exchange Server 인증서 때문에 Windows Mobile 디바이스와 ActiveSync가 되지 않는 경우 해결법
  • PC 부팅할때 Num Lock 키 해제하는 방법
  • MFC에서 디스크 포멧(Disk Format)하기
  • 소켓을 생성하여 CSocket에 Attach하는 방법
  • Java로 구현하는 간단한 Client & Server 프로그램
  • 시간 알아내는 함수 gmtime
  • 글자 깜빡이게 하기
  • PHP에서 URL Open시 에러가 날때
  • php로 년,월,일 요일 구하기
    Tagged on:                         
  • Leave a Reply