#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <arpa/inet.h>
#define MAXBUF 256
int main() {
int ssock, csock;
int clen;
struct sockaddr_in client_addr, server_addr;
char buf[MAXBUF] = "i like you!";
if ((ssock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0) {
perror("socket error :");
exit(1);
}
clen = sizeof(client_addr);
memset(&server_addr, 0, sizeof(server_addr));
server_addr.sin_family = AF_INET;
server_addr.sin_addr.s_addr = htonl(INADDR_ANY);
server_addr.sin_port = htons(3317);
if (bind(ssock, (struct sockaddr *) &server_addr, sizeof(server_addr)) < 0) {
perror("socket error :");
exit(1);
}
if (listen(ssock, 8) < 0) {
perror("socket error :");
exit(1);
}
while (1) {
csock = accept(ssock, (struct sockaddr *) &client_addr, &clen);
if (write(csock, buf, MAXBUF) <= 0) {
perror("socket error : ");
}
close(csock);
}
return 0;
}
#include <stdio.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <arpa/inet.h>
#define MAXBUF 256
int main() {
int ssock;
int clen;
struct sockaddr_in server_addr;
char buf[MAXBUF];
if ((ssock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0) {
perror("socket error : ");
exit(1);
}
clen = sizeof(server_addr);
memset(&server_addr, 0, sizeof(server_addr));
server_addr.sin_family = AF_INET;
server_addr.sin_addr.s_addr = inet_addr("127.0.0.1");
server_addr.sin_port = htons(3317);
if (connect(ssock, (struct sockaddr *) &server_addr, clen) < 0) {
perror("connect error : " );
exit(1);
}
memset(buf, 0, MAXBUF);
if (read(ssock, buf, MAXBUF) <= 0) {
perror("read error : ");
exit(0);
}
close(ssock);
printf("\nread : %s\n\n", buf);
return 0;
}