TCP
server.c
c
#include <myhead.h>
#define PORT 8888
#define IP "192.168.0.102"
int main(int argc, const char *argv[])
{
int sfd = socket(AF_INET, SOCK_STREAM, 0);
if(sfd == -1){
perror("socket error");
return -1;
}
int reuse = -1;
if(setsockopt(sfd, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse)) == -1){
perror("setsockopt error");
return -1;
}
struct sockaddr_in sin;
sin.sin_family = AF_INET;
sin.sin_port = htons(PORT);
sin.sin_addr.s_addr = inet_addr(IP);
if(bind(sfd, (struct sockaddr *)&sin, sizeof(sin)) == -1){
perror("bind error");
return -1;
}
if(listen(sfd, 128) == -1){
perror("listen error");
return -1;
}
struct sockaddr_in cin;
socklen_t socklen = sizeof(cin);
int newfd = accept(sfd, (struct sockaddr *)&cin, &socklen);
if(newfd == -1){
perror("accept error");
return -1;
}
char buf[128];
while(1){
bzero(buf, sizeof(buf));
int res = recv(newfd, buf, sizeof(buf), 0);
if(res == 0){
puts("客户端已下线");
break;
}
printf("[%s: %d] %s\n", inet_ntoa(cin.sin_addr), ntohs(cin.sin_port), buf);
strcat(buf, "*_*");
send(newfd, buf, sizeof(buf), 0);
puts("发送成功");
}
close(newfd);
close(sfd);
return 0;
}
client.c
c
#include <myhead.h>
#define SPORT 8888
#define SIP "192.168.0.102"
#define CPORT 9999
int main(int argc, const char *argv[])
{
int cfd = socket(AF_INET, SOCK_STREAM, 0);
if(cfd == -1){
perror("socket error");
return -1;
}
int reuse = -1;
if(setsockopt(cfd, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse)) == -1){
perror("setsockopt error");
return -1;
}
struct sockaddr_in cin;
cin.sin_family = AF_INET;
cin.sin_port = htons(CPORT);
cin.sin_addr.s_addr = inet_addr(SIP);
if(bind(cfd, (struct sockaddr *)&cin, sizeof(cin)) == -1){
perror("bind error");
return -1;
}
struct sockaddr_in sin;
sin.sin_family = AF_INET;
sin.sin_port = htons(SPORT);
sin.sin_addr.s_addr = inet_addr(SIP);
if(connect(cfd, (struct sockaddr *)&sin, sizeof(sin)) == -1){
perror("connect error");
return -1;
}
char sbuf[128] = "hello";
char rbuf[128];
while(1){
send(cfd, sbuf, sizeof(sbuf), 0);
bzero(rbuf, sizeof(rbuf));
recv(cfd, rbuf, sizeof(rbuf), 0);
printf("[%s: %d] %s\n", inet_ntoa(sin.sin_addr), ntohs(sin.sin_port), rbuf);
sleep(1);
}
close(cfd);
return 0;
}
UDP
server.c
c
#include <myhead.h>
#define PORT 8888
#define IP "192.168.0.102"
int main(int argc, const char *argv[])
{
int sfd = socket(AF_INET, SOCK_DGRAM, 0);
if(sfd == -1){
perror("socket error");
return -1;
}
int reuse = -1;
if(setsockopt(sfd, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse)) == -1){
perror("setsockopt error");
return -1;
}
struct sockaddr_in sin;
sin.sin_family = AF_INET;
sin.sin_port = htons(PORT);
sin.sin_addr.s_addr = inet_addr(IP);
if(bind(sfd, (struct sockaddr *)&sin, sizeof(sin)) == -1){
perror("bind error");
return -1;
}
struct sockaddr_in cin;
socklen_t socklen = sizeof(cin);
char buf[128];
while(1){
bzero(buf, sizeof(buf));
recvfrom(sfd, buf, sizeof(buf), 0, (struct sockaddr *)&cin, &socklen);
printf("[%s: %d] %s\n", inet_ntoa(cin.sin_addr), ntohs(cin.sin_port), buf);
strcat(buf, "*_*");
sendto(sfd, buf, sizeof(buf), 0, (struct sockaddr *)&cin, socklen);
}
close(sfd);
return 0;
}
client.c
c
#include <myhead.h>
#define PORT 8888
#define IP "192.168.0.102"
int main(int argc, const char *argv[])
{
int cfd = socket(AF_INET, SOCK_DGRAM, 0);
if(cfd == -1){
perror("socket error");
return -1;
}
int reuse = -1;
if(setsockopt(cfd, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse)) == -1){
perror("setsockopt error");
return -1;
}
struct sockaddr_in sin;
sin.sin_family = AF_INET;
sin.sin_port = htons(PORT);
sin.sin_addr.s_addr = inet_addr(IP);
socklen_t socklen = sizeof(sin);
if(connect(cfd, (struct sockaddr *)&sin, sizeof(sin)) == -1){
perror("connect error");
return -1;
}
char buf[128] = "hello";
while(1){
sendto(cfd, buf, sizeof(buf), 0, (struct sockaddr *)&sin, socklen);
recvfrom(cfd, buf, sizeof(buf), 0, NULL, NULL);
printf("[%s: %d] %s\n", inet_ntoa(sin.sin_addr), ntohs(sin.sin_port), buf);
sleep(1);
}
close(cfd);
return 0;
}