Socket Programming
BSD Sockets

// simple_socket.c
#include <sys/types.h>
#include <sys/socket.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#define DATA1 "This data is sent from a child process . . ."
#define DATA2 "This data is sent from a parent process . . ."
/*
* This program creates a pair of connected sockets,
* then forks and communicates over them. This is very
* similar to communication with pipes, however, socketpairs
* are two-way communications objects. Therefore I can
* send messages in both directions.
*/
main() {
int sockets[2], child;
char buf[1024];
if (socketpair(AF_UNIX, SOCK_STREAM, 0, sockets) < 0) {
perror("opening stream socket pair");
exit(1);
}
if ((child = fork()) == -1)
perror("fork");
else if (child) { /* This is the parent. */
close(sockets[0]);
if (read(sockets[1], buf, sizeof(buf)) < 0)
perror("reading stream message");
printf("-->%s\n", buf);
if (write(sockets[1], DATA2, sizeof(DATA2)) < 0)
perror("writing stream message");
close(sockets[1]);
} else { /* This is the child. */
close(sockets[1]);
if (write(sockets[0], DATA1, sizeof(DATA1)) < 0)
perror("writing stream message");
if (read(sockets[0], buf, sizeof(buf)) < 0)
perror("reading stream message");
printf("-->%s\n", buf);
close(sockets[0]);
}
}Socket นั้นมีอยู่ด้วยกัน 3 ประเภทโดยยึดตามมาตราฐานของเทคโนโลยี TCP/IP ได้แก่
Socket
คำอธิบาย


ประเภทของการเชื่อมต่อสื่อสารระหว่างเครื่องคอมพิวเตอร์ผ่าน socket นั้นแบ่งออกได้เป็น 2 ประเภทได้แก่
Last updated
Was this helpful?