TCP服务器
cs
#include "head.h"
#define PORT 2580 //端口号
#define IP "192.168.31.219" //本机IP
int main(int argc, const char *argv[])
{
sqlite3* db=NULL;
if(sqlite3_open("./my.db",&db)!=SQLITE_OK)
{
fprintf(stderr,"sqlite3_open %d : %s __%d__\n",\
sqlite3_errcode(db),sqlite3_errmsg(db),__LINE__);
return -1;
}
printf("database open success __%d__\n",__LINE__);
//创建一张表格
char sql[128]="create table if not exists stu (name char,password int);";
char* errmsg=NULL;
if(sqlite3_exec(db,sql,NULL,NULL,&errmsg)!=SQLITE_OK)
{
fprintf(stderr,"sqlite3_exec: %s __%d__\n",errmsg,__LINE__);
return -1;
}
printf("create table stu success\n");
//创建流式套接字
int sfd = socket(AF_INET,SOCK_STREAM,0);
if(sfd<0)
{
ERR_MSG("socket");
return -1;
}
printf("socket create success\n");
//允许端口快速复用
int reuse = 1;
if(setsockopt(sfd, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse)) < 0)
{
ERR_MSG("setsockopt");
return -1;
}
printf("允许端口快速复用成功\n");
//填充地址信息结构体给bind函数绑定使用
//真实地址信息结构体根据地质族指定,AF_INET;man 7 ip
struct sockaddr_in sin;
sin.sin_family = AF_INET; //必须填AF_INET;
sin.sin_port = htons(PORT); //端口号网络字节序,1024-49151
sin.sin_addr.s_addr = inet_addr(IP); //本机IP的网络字节序,ifconfig
//绑定服务器的地址信息》必须绑定
if(bind(sfd,(struct sockaddr*)&sin,sizeof(sin))<0)
{
ERR_MSG("bind");
return -1;
}
printf("bind success\n");
//将套接字转换成被动监听状态
if(listen(sfd,128)<0)
{
ERR_MSG("listen");
return -1;
}
printf("listen success\n");
struct sockaddr_in cin;
socklen_t addrlen = sizeof(cin);
//获取连接成功的客户端信息,生成新的文件描述符
//该文件描述符才是与客户端通信的文件描述符
//int newfd = accept(sfd,NULL,NULL);
int newfd = accept(sfd,(struct sockaddr*)&cin,&addrlen);
if(newfd<0)
{
ERR_MSG("accept");
return -1;
}
printf("[%s:%d] newfd =%d 客户端连接成功__%d__\n",inet_ntoa(cin.sin_addr),ntohs(cin.sin_port),newfd,__LINE__);
char buf[128]="";
ssize_t res=0;
int flag=0;
while(1)
{
bzero(buf,sizeof(buf));
//接收数据
res=recv(newfd,buf,sizeof(buf),0);
if(res<0)
{
ERR_MSG("recv");
return -1;
}
else if(0==res)
{
printf("[%s:%d] newfd=%d : 客户端下线 __%d__\n",\
inet_ntoa(cin.sin_addr),ntohs(cin.sin_port),newfd,__LINE__);
break;
}
int i;
char str[128]="";
for(i=0;buf[i]!=' ';i++)
{
str[i]=buf[i];
}
str[i]=buf[i];
char sql[128]="select name from stu;";
char **pre=NULL;
int row,column;
char* errmsg=NULL;
if(sqlite3_get_table(db,sql,&pre,&row,&column,&errmsg)!=SQLITE_OK)
{
fprintf(stderr,"sqlite3_get_table: %s \n",errmsg);
return -1;
}
for(int i=0;i<row+1;i++)
{
for(int j=0;j<column;j++)
{
if(strcmp(str,pre[(i*column)+j])==0)
{
printf("重复注册\n");
flag=1;
}
}
}
if(flag==0){
sqlite3_free_table(pre);
char sql1[128]="";
char* p1=buf+strlen(str);
sprintf(sql1,"insert into stu values(\"%s\",\"%s\");",str,p1);
char* errmsg1 = NULL;
if(sqlite3_exec(db,sql1,NULL,NULL,&errmsg1)!=SQLITE_OK)
{
fprintf(stderr,"sqlite3_exec: %s __%d__\n",errmsg1,__LINE__);
return -1;
}
printf("insert success\n");
if(strcmp(buf,"quit")==0)
break;
//发送数据
strcat(buf," add success");
if(send(newfd,buf,sizeof(buf),0)<0)
{
ERR_MSG("send");
return -1;
}
printf("send success\n");
}
else if(flag==1)
break;
}
//关闭套接字
close(sfd);
close(newfd);
return 0;
}
TCP客户端
cs
#include "head.h"
#define SER_PORT 2580 //端口号
#define SER_IP "192.168.31.219" //本机IP
int main(int argc, const char *argv[])
{
//创建流式套接字
int sfd = socket(AF_INET,SOCK_STREAM,0);
if(sfd<0)
{
ERR_MSG("socket");
return -1;
}
printf("socket create success\n");
//允许端口快速复用
int reuse = 1;
if(setsockopt(sfd, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse)) < 0)
{
ERR_MSG("setsockopt");
return -1;
}
printf("允许端口快速复用成功\n");
//填充地址信息结构体给bind函数绑定使用
//真实地址信息结构体根据地质族指定,AF_INET;man 7 ip
struct sockaddr_in sin;
sin.sin_family = AF_INET; //必须填AF_INET;
sin.sin_port = htons(SER_PORT); //端口号网络字节序,1024-49151
sin.sin_addr.s_addr = inet_addr(SER_IP); //本机IP的网络字节序,ifconfig
if(connect(sfd,(struct sockaddr*)&sin,sizeof(sin))<0)
{
ERR_MSG("connect");
return -1;
}
printf("connect success\n");
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(sfd,buf,sizeof(buf),0)<0)
{
ERR_MSG("send");
return -1;
}
printf("send success\n");
if(strcmp(buf,"quit")==0)
break;
bzero(buf,sizeof(buf));
//接收数据
res=recv(sfd,buf,sizeof(buf),0);
if(res<0)
{
ERR_MSG("recv");
return -1;
}
else if(0==res)
{
printf("[%s:%d] newfd=%d : 服务器下线 __%d__\n",\
SER_IP,SER_PORT,sfd,__LINE__);
break;
}
printf("[%s:%d] newfd=%d : %s __%d__\n",\
SER_IP,SER_PORT,sfd,buf,__LINE__);
}
//关闭套接字
close(sfd);
return 0;
}