#include<myhead.h>
#define IP "192.168.174.133"
#define PORT 7788
typedef struct msg_t
{
}msg_t;
void *handle_client(void *newfd)
{
ssize_t res=-1;
msg_t msg;
res=recv(*(int *)newfd,&msg,sizeof(msg),0);
if(res<0)
{
ERR_MSG("recv");
}
else//处理信息
{
}
}
int main(int argc, const char *argv[])
{
int sfd;
if((sfd=socket(AF_INET,SOCK_STREAM,0))<0)
{
ERR_MSG("socket");
return 0;
}
int reuse=1;
if(setsockopt(sfd,SOL_SOCKET,SO_REUSEADDR,&reuse,sizeof(reuse))<0)
{
ERR_MSG("setsockopt");
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(bind(sfd,(struct sockaddr *)(&sin),socklen)!=0)
{
ERR_MSG("bind");
return 0;
}
if(listen(sfd,128)!=0)
{
ERR_MSG("listen");
return -1;
}
struct sockaddr_in cin;
socklen=sizeof(cin);
int newfd=-1;
int tid;
while(1)
{
if((newfd=accept(sfd,(struct sockaddr *)(&cin),&socklen))<0)
{
ERR_MSG("accept");
}
pthread_t tid;
if((pthread_create(&tid,NULL,handle_client,&newfd))!=0)
{
ERR_MSG("pthread_create");
}
pthread_detach(tid);
}
return 0;
}
#include<myhead.h>
#include"fun.h"
#define IP "192.168.174.132"
#define PORT 8888
int main(int argc, const char *argv[])
{
//----------打开数据库-----------
sqlite3 *pic;
if(sqlite3_open("./pic.db",&pic)!=SQLITE_OK)
{
fprintf(stderr,"sqlite3_open %s %d __%d__\n",sqlite3_errmsg(pic),sqlite3_errcode(pic),__LINE__);
}
//---------添加表----------------
char *errmsg;
char opt[128]="create table if not exists pic(ID int PRIMAERY KEY,image BLOB);";
if(sqlite3_exec(pic,opt,NULL,NULL,&errmsg)!=SQLITE_OK)
{
fprintf(stderr,"sqlite3_exec %s %d __%d__",sqlite3_errmsg(pic),sqlite3_errcode(pic),__LINE__);
}
//----------创建套接字------------
int sfd;
if((sfd=socket(AF_INET,SOCK_STREAM,0))<0)
{
ERR_MSG("socket");
return -1;
}
printf("套接字创建成功\n");
//----------端口复用--------------
int reuse=1;
if(setsockopt(sfd,SOL_SOCKET,SO_REUSEADDR,&reuse,sizeof(reuse))<0)
{
ERR_MSG("setsockopt");
return -1;
}
printf("允许端口复用成功\n");
//----------bind--------------
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(bind(sfd,(struct sockaddr *)(&sin),socklen)!=0)
{
ERR_MSG("bind");
return -1;
}
printf("绑定成功\n");
//--------listen------------
if(listen(sfd,128)!=0)
{
ERR_MSG("listen\n");
return -1;
}
printf("listen 成功\n");
//------------------------------------------------------------------------------
int newfd=-1;
struct sockaddr_in cin;
socklen=sizeof(cin);
pid_t cpid=-1;
while(1)
{
newfd=accept(sfd,(struct sockaddr *)(&cin),&socklen);
if(newfd<0)
{
ERR_MSG("accept");
return -1;
}
printf("客户端连接%s:%d\n",inet_ntoa(cin.sin_addr),ntohs(cin.sin_port));
cpid=fork();
//------------------------------------------------------------------------------
if(cpid==0)
{
close(sfd);
//客户端交互
msg_t msg;
ssize_t res;
while(1)
{
bzero(&msg,sizeof(msg));
res=recv(newfd,&msg,sizeof(msg),0);
if(res<0)
{
ERR_MSG("recv");
}
else if(res==0)
{
printf("客户端下线%s:%d\n",inet_ntoa(cin.sin_addr),ntohs(cin.sin_port));
}
else//判断消息内容并处理
{
}
}
exit(0);
}
close(newfd);
}
if(close(sfd)!=0)
{
ERR_MSG("close");
return -1;
}
return 0;
}