//tcp客户端代码
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <stdint.h>
#include <sys/socket.h>
#include <netinet/in.h>
#define ERR_MGS(m)do{\
fprintf(stderr,"%d\n",__LINE__);\
perror(m);\
}while(0)
int main(int argc, const char *argv[])
{
//基于TCP的客户端模型
//创建套接字
int scoketd=socket(AF_INET,SOCK_STREAM,0);
if(-1==scoketd){
ERR_MGS("scoket");
return 0;
}
//允许端口快速重用
int reuse = 1;
if(setsockopt(scoketd, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse))<0)
{
ERR_MGS("setsockopt");
return -1;
}
printf("创建套接字成功\n");
//绑定客户端的ip和端口
//创建结构体变量并填充结构体
struct sockaddr_in cin;
cin.sin_family=AF_INET;
cin.sin_port=htons(6666);
cin.sin_addr.s_addr=inet_addr("192.168.8.214");
socklen_t addrlen=sizeof(cin);
int fd=bind(scoketd,(struct sockaddr*)&cin,addrlen);
if(-1==fd){
ERR_MGS("bind");
return 0;
}
printf("绑定成功\n");
//连接到服务器
struct sockaddr_in sin;
sin.sin_family=AF_INET;
sin.sin_port=htons(8888);
sin.sin_addr.s_addr=inet_addr("192.168.8.214");
socklen_t addrlen1=sizeof(sin);
int fpd=connect(scoketd,(struct sockaddr*)&sin,addrlen1);
if(-1==fpd){
ERR_MGS("accept");
return 0;
}
printf("%s | %d]",inet_ntoa(sin.sin_addr),ntohl(sin.sin_port));
//发送信息给服务器
ssize_t res;
char buf[128];
while(1){
//发送信息给服务器
printf("请输入内容>>>\n");
bzero(buf,sizeof(buf));
fgets(buf,sizeof(buf),stdin);
buf[strlen(buf)-1]='\0';
res=send(scoketd,buf,sizeof(buf),0);
if(-1==res){
ERR_MGS("send");
break;
}
//接收服务器信息
bzero(buf,sizeof(buf));
res=recv(fpd,buf,sizeof(buf),0);
if(res==0){
printf("对端关闭\n");
break;
}else if(-1==res){
ERR_MGS("recv");
break;
}
printf("%s | %d] %s\n",inet_ntoa(sin.sin_addr),ntohl(sin.sin_port),buf);
}
//关闭文件描述符
close(scoketd);
return 0;
}
//udp的服务器
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <stdint.h>
#include <sys/socket.h>
#include <netinet/in.h>
#define ERR_MGS(m)do{\
fprintf(stderr,"%d\n",__LINE__);\
perror(m);\
}while(0)
int main(int argc, const char *argv[])
{
//基于UDP的服务器模型
//创建套接字
int scoketd=socket(AF_INET,SOCK_DGRAM,0);
if(-1==scoketd){
ERR_MGS("scoket");
return 0;
}
//允许端口快速重用
int reuse = 1;
if(setsockopt(scoketd, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse))<0)
{
ERR_MGS("setsockopt");
return -1;
}
printf("创建套接字成功\n");
//绑定服务器的ip和端口
//创建结构体变量并填充结构体
struct sockaddr_in cin;
cin.sin_family=AF_INET;
cin.sin_port=htons(8888);
cin.sin_addr.s_addr=inet_addr("192.168.8.214");
socklen_t addrlen=sizeof(cin);
int fd=bind(scoketd,(struct sockaddr*)&cin,addrlen);
if(-1==fd){
ERR_MGS("bind");
return 0;
}
printf("绑定成功\n");
//接收客户端的信息
ssize_t res;
char buf[128];
struct sockaddr_in sind;
socklen_t addrlen1=sizeof(sind);
while(1){
bzero(buf,sizeof(buf));
res=recvfrom(scoketd,buf,sizeof(buf),0,(struct sockaddr*)&sind,&addrlen1);
if(-1==res){
ERR_MGS("recvfrom");
break;
}
printf("[%s | %d] %s\n",inet_ntoa(sind.sin_addr),ntohs(sind.sin_port),buf);
//发送信息给客户端
strcat(buf,"*_*");
res=sendto(scoketd,buf,sizeof(buf),0,(struct sockaddr*)&sind,addrlen1);
if(-1==res){
ERR_MGS("sendto");
break;
}
printf("发送信息成功\n");
}
//关闭文件描述符
close(scoketd);
return 0;
}
//udp的客户端
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <stdint.h>
#include <sys/socket.h>
#include <netinet/in.h>
#define ERR_MGS(m)do{\
fprintf(stderr,"%d\n",__LINE__);\
perror(m);\
}while(0)
int main(int argc, const char *argv[])
{
//基于UDP的客户端模型
//创建套接字
int scoketd=socket(AF_INET,SOCK_DGRAM,0);
if(-1==scoketd){
ERR_MGS("scoket");
return 0;
}
//允许端口快速重用
int reuse = 1;
if(setsockopt(scoketd, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse))<0)
{
ERR_MGS("setsockopt");
return -1;
}
printf("创建套接字成功\n");
//绑定客户端的ip和端口
//创建结构体变量并填充结构体
struct sockaddr_in cin;
cin.sin_family=AF_INET;
cin.sin_port=htons(6666);
cin.sin_addr.s_addr=inet_addr("192.168.8.214");
socklen_t addrlen=sizeof(cin);
int fd=bind(scoketd,(struct sockaddr*)&cin,addrlen);
if(-1==fd){
ERR_MGS("bind");
return 0;
}
printf("绑定成功\n");
//向服务器的发送信息
ssize_t res;
char buf[128];
struct sockaddr_in sind;
sind.sin_family=AF_INET;
sind.sin_port=htons(8888);
sind.sin_addr.s_addr=inet_addr("192.168.8.214");
socklen_t addrlen1=sizeof(sind);
while(1){
printf("请输入>>>\n");
bzero(buf,sizeof(buf));
fgets(buf,sizeof(buf),stdin);
buf[strlen(buf)-1]=0;
res=sendto(scoketd,buf,sizeof(buf),0,(struct sockaddr*)&sind,addrlen1);
if(-1==res){
ERR_MGS("sendto");
break;
}
printf("发送信息成功\n");
//接收服务器回应的信息
bzero(buf,sizeof(buf));
res=recvfrom(scoketd,buf,sizeof(buf),0,(struct sockaddr*)&sind,&addrlen1);
if(-1==res){
ERR_MGS("recvfrom");
break;
}
printf("收到回复:[%s | %d] %s\n",inet_ntoa(sind.sin_addr),ntohs(sind.sin_port),buf);
}
//关闭文件描述符
close(scoketd);
return 0;
}