最基本的TCP/IP通信
server
#include <iostream>
#include <sys/socket.h>
#include <netinet/in.h>
#include <unistd.h>
#include <string.h>
#define PORT 8111
#define MESSAGE_SIZE 1024
int main(int argc, char* argv[])
{
int socket_fd;
int accept_fd;
int backlog = 10;
int ret = -1;
int flag = 1;
struct sockaddr_in local_addr, remote_addr;
char in_buf[MESSAGE_SIZE] = {
0,};
socket_fd = socket(AF_INET, SOCK_STREAM, 0);
if (socket_fd == -1)
{
std::cout << "file to create socket!" << std::endl;
exit(-1);
}
ret = setsockopt(socket_fd,
SOL_SOCKET,
SO_REUSEADDR,
&flag,
sizeof(flag));
if (ret == -1)
{
std::cout << "failed to set socket options!" << std::endl;
}
local_addr.sin_family = AF_INET;
local_addr.sin_port = PORT;
local_addr.sin_addr.s_addr = INADDR_ANY;
bzero(&(local_addr.sin_zero),8);
ret = bind(socket_fd, (struct sockaddr *)&local_addr, sizeof(struct sockaddr));
if (ret == -1)
{
std::cout << "failed to bind addr!" << std::endl;
exit(-1);
}
ret = listen(socket_fd, backlog);
if (ret == -1)
{
std::cout << "failed to listen socket!" << std::endl;
exit(-1);
}
for(;;)
{
socklen_t addr_len = sizeof(struct sockaddr);
accept_fd = accept(socket_fd,
(struct sockaddr *) &remote_addr,
&addr_len);
for (;;)
{
ret = recv(accept_fd, (void *)in_buf, MESSAGE_SIZE, 0);
if (ret == 0)
break;
std::cout << "receive: " << in_buf << std::endl;
send(accept_fd, (void*)in_buf, MESSAGE_SIZE,0);
}
close(accept_fd);
}
close(socket_fd);
return 0;
}
client
#include <iostream>
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <string.h>
#include <stdio.h>
#include <unistd.h>
#include <arpa/inet.h>
#define PORT 8111
#define MESSAGE_LEN 1024
using namespace std;
int main(int argc, char* argv[])
{
int socket_fd;
int ret = -1;
char sendbuf[MESSAGE_LEN] = {
0,};
char recvbuf[MESSAGE_LEN] = {
0,};
struct sockaddr_in serverAddr;
socket_fd = socket(AF_INET, SOCK_STREAM, 0);
if (socket_fd < 0)
{
cout << "failed to create socket" << endl;
exit(-1);
}
serverAddr.sin_family = AF_INET;
serverAddr.sin_port = PORT;
serverAddr.sin_addr.s_addr = inet_addr("127.0.0.1");
ret = connect(socket_fd,
(struct sockaddr *)&serverAddr,
sizeof(struct sockaddr));
if (ret < 0)
{
cout << "failed to connect!" << endl;
exit(-1);
}
while(1)
{
memset(sendbuf, 0, MESSAGE_LEN);
gets(sendbuf);
ret = send(socket_fd, sendbuf, strlen(sendbuf), 0);
if (ret <= 0)
{
cout << "failed to send data!" << endl;
break;
}
if (strcmp(sendbuf, "quit") == 0)
{
break;
}
ret = recv(socket_fd, recvbuf, MESSAGE_LEN, 0);
recvbuf[ret] = '\0';
cout << "recv:" << recvbuf << endl;
}
close(socket_fd);
return 0;
}
fork方式
server
#include <iostream>
#include <sys/socket.h>
#include <netinet/in.h>
#include <unistd.h>
#include <string.h>
#define PORT 8111
#define MESSAGE_SIZE 1024
int main(int argc, char* argv[])
{
int socket_fd;
int accept_fd;
int backlog = 10;
int ret = -1;
int flag = 1;
pid_t pid;
struct sockaddr_in local_addr, remote_addr;
char in_buf[MESSAGE_SIZE] = {
0,};
socket_fd = socket(AF_INET, SOCK_STREAM, 0);
if (socket_fd == -1)
{
std::cout << "file to create socket!" << std::endl;
exit(-1);
}
ret = setsockopt(socket_fd,
SOL_SOCKET,
SO_REUSEADDR,
&flag,
sizeof(flag));
if (ret == -1)
{
std::cout << "failed to set socket options!" << std::endl;
}
local_addr.sin_family = AF_INET;
local_addr.sin_port = PORT;
local_addr.sin_addr.s_addr = INADDR_ANY;
bzero(&(local_addr.sin_zero),8);
ret = bind(socket_fd, (struct sockaddr *)&local_addr, sizeof(struct sockaddr));
if (ret == -1)
{
std::cout &