TCP
服务器端
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define IP "192.168.8.225"
#define PORT 1024
int main()
{
int serve_fd = socket(AF_INET, SOCK_STREAM, 0);
if (serve_fd < 0)
{
perror("socket");
return -1;
}
int resue = 1;
if (setsockopt(serve_fd, SOL_SOCKET, SO_REUSEADDR, &resue, sizeof(resue)) < 0)
{
perror("setsockopt");
return -1;
}
struct sockaddr_in server_in;
server_in.sin_family = AF_INET;
server_in.sin_port = htons(PORT);
server_in.sin_addr.s_addr = inet_addr(IP);
if (bind(serve_fd, (struct sockaddr *)&server_in, sizeof(server_in)) < 0)
{
perror("bind");
return -1;
}
if (listen(serve_fd, 128) < 0)
{
perror("listen");
return -1;
}
printf("listen success __%d__\n", __LINE__);
struct sockaddr_in client_fd;
socklen_t addrlent = sizeof(client_fd);
int serve_fd_new = accept(serve_fd, (struct sockaddr *)&client_fd, &addrlent);
if (serve_fd_new < 0)
{
perror("accept");
return -1;
}
printf("[%s:%d] newfd=%d 连接成功:__%d__\n", inet_ntoa(client_fd.sin_addr),
ntohs(client_fd.sin_port), serve_fd_new, __LINE__);
char buf[128] = "";
ssize_t res = 0;
while (1)
{
bzero(buf, sizeof(buf));
res = recv(serve_fd_new, buf, sizeof(buf), 0);
if (res < 0)
{
perror("recv");
return -1;
}
else if (0 == res)
{
printf("[%s:%d] newfd=%d 客户端下线:__%d__\n", inet_ntoa(client_fd.sin_addr),
ntohs(client_fd.sin_port), serve_fd_new, __LINE__);
break;
}
printf("newfd=%d: %s\n", serve_fd_new, buf);
printf("请输入要发送的消息>>>");
fgets(buf, sizeof(buf), stdin);
buf[strlen(buf) - 1] = 0;
if (send(serve_fd_new, buf, sizeof(buf), 0) < 0)
{
perror("send");
return -1;
}
printf("发送成功\n");
}
close(serve_fd);
close(serve_fd_new);
return 0;
}
客户端
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define IP "192.168.8.225"
#define PORT 1024
int main()
{
int client_fd = socket(AF_INET, SOCK_STREAM, 0);
if (client_fd < 0)
{
perror("socket");
return -1;
}
int resue = 1;
if (setsockopt(client_fd, SOL_SOCKET, SO_REUSEADDR, &resue, sizeof(resue)) < 0)
{
perror("setsockopt");
return -1;
}
struct sockaddr_in client_in;
client_in.sin_family = AF_INET;
client_in.sin_port = htons(PORT);
client_in.sin_addr.s_addr = inet_addr(IP);
#if 0
if (bind(client_fd, (struct sockaddr *)&server_in, sizeof(server_in)) < 0)
{
perror("bind");
return -1;
}
#endif
if(connect(client_fd,(struct sockaddr*)&client_in,sizeof(client_in))<0)
{
perror("connect");
return -1;
}
printf("connect success __%d__\n",__LINE__);
char buf[128] = "";
ssize_t res = 0;
while (1)
{
bzero(buf,sizeof(buf));
printf("请输入>>>");
fgets(buf,sizeof(buf),stdin);
buf[strlen(buf)-1]=0;
if (send(client_fd, buf, sizeof(buf), 0) < 0)
{
perror("send");
return -1;
}
printf("发送成功\n");
bzero(buf, sizeof(buf));
res = recv(client_fd, buf, sizeof(buf), 0);
if (res < 0)
{
perror("recv");
return -1;
}
else if (0 == res)
{
printf("client_fd=%d 服务器下线:__%d__\n", client_fd, __LINE__);
break;
}
printf("client_fd=%d: %s\n", client_fd, buf);
}
close(client_fd);
return 0;
}
UDP
服务器端
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define IP "192.168.8.225"
#define PORT 1024
int main()
{
int serve_fd = socket(AF_INET, SOCK_DGRAM, 0);
if (serve_fd < 0)
{
perror("socket");
return -1;
}
int resue = 1;
if (setsockopt(serve_fd, SOL_SOCKET, SO_REUSEADDR, &resue, sizeof(resue)) < 0)
{
perror("setsockopt");
return -1;
}
struct sockaddr_in server_in;
server_in.sin_family = AF_INET;
server_in.sin_port = htons(PORT);
server_in.sin_addr.s_addr = inet_addr(IP);
if (bind(serve_fd, (struct sockaddr *)&server_in, sizeof(server_in)) < 0)
{
perror("bind");
return -1;
}
struct sockaddr_in client_in;
socklen_t addrlen = sizeof(client_in);
char buf[128] = "";
while (1)
{
bzero(buf, sizeof(buf));
if (recvfrom(serve_fd, buf, sizeof(buf), 0, (struct sockaddr *)&client_in, &addrlen) < 0)
{
perror("recvfrom");
return -1;
}
printf("[%s : %d]: %s\n", inet_ntoa(client_in.sin_addr), ntohs(client_in.sin_port), buf);
printf("发送>>>");
fgets(buf, sizeof(buf), stdin);
buf[strlen(buf) - 1] = 0;
if (sendto(serve_fd, buf, sizeof(buf), 0, (struct sockaddr *)&client_in, sizeof(client_in)) < 0)
{
perror("sendto");
return -1;
}
printf("发送成功\n");
}
close(serve_fd);
return 0;
}
客户端
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define IP "192.168.8.225"
#define PORT 1024
int main()
{
int client_fd = socket(AF_INET, SOCK_DGRAM, 0);
if (client_fd < 0)
{
perror("socket");
return -1;
}
int resue = 1;
if (setsockopt(client_fd, SOL_SOCKET, SO_REUSEADDR, &resue, sizeof(resue)) < 0)
{
perror("setsockopt");
return -1;
}
#if 0
if (bind(client_fd, (struct sockaddr *)&client_in, sizeof(client_in)) < 0)
{
perror("bind");
return -1;
}
#endif
struct sockaddr_in serve_in;
serve_in.sin_family = AF_INET;
serve_in.sin_port = htons(PORT);
serve_in.sin_addr.s_addr = inet_addr(IP);
struct sockaddr_in client_in;
socklen_t addrlen = sizeof(client_in);
char buf[128] = "";
while (1)
{
printf("发送>>>");
fgets(buf, sizeof(buf), stdin);
buf[strlen(buf) - 1] = 0;
if (sendto(client_fd, buf, sizeof(buf), 0, (struct sockaddr *)&serve_in, sizeof(serve_in)) < 0)
{
perror("sendto");
return -1;
}
printf("发送成功\n");
bzero(buf, sizeof(buf));
if (recvfrom(client_fd, buf, sizeof(buf), 0, (struct sockaddr *)&client_in, &addrlen) < 0)
{
perror("recvfrom");
return -1;
}
printf("[%s : %d]: %s\n", inet_ntoa(client_in.sin_addr), ntohs(client_in.sin_port), buf);
}
close(client_fd);
return 0;
}