server.c
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<netdb.h>
#include<string.h>
#include "sqlite3.h"
int port=7777;
int main()
{
printf("[%s, %s, %d]\n", __FILE__, __FUNCTION__, __LINE__);
printf("main!\n");
struct sockaddr_in sin;
struct sockaddr_in pin;
int sock_descriptor;
int temp_sock_descriptor;
int size_of_addr;
char buf[100000];
char buf2[100000];
memset(buf2,' ',sizeof(buf2));
memset(buf,' ',sizeof(buf));
char sendmsg[1024];
memset(sendmsg,' ',sizeof(sendmsg));
printf("[%s, %s, %d]\n", __FILE__, __FUNCTION__, __LINE__);
int i, length;
sock_descriptor=socket(AF_INET, SOCK_STREAM, 0);
printf("[%s, %s, %d]\n", __FILE__, __FUNCTION__, __LINE__);
if(sock_descriptor == -1)
{
perror("socket!");
exit(1);
}
printf("[%s, %s, %d]\n", __FILE__, __FUNCTION__, __LINE__);
bzero(&sin, sizeof(sin));
sin.sin_family=AF_INET;
sin.sin_addr.s_addr = INADDR_ANY;
sin.sin_port=htons(port);
printf("[%s, %s, %d]\n", __FILE__, __FUNCTION__, __LINE__);
if(bind(sock_descriptor, (struct sockaddr*)&sin, sizeof(sin)) == -1)
{
perror("bind");
exit(1);
}
printf("[%s, %s, %d]\n", __FILE__, __FUNCTION__, __LINE__);
if(listen(sock_descriptor, 20) == -1)
{
perror("listen1");
exit(1);
}
printf("[%s, %s, %d]\n", __FILE__, __FUNCTION__, __LINE__);
printf("waiting for accepting!\n");
printf("[%s, %s, %d]\n", __FILE__, __FUNCTION__, __LINE__);
while(1)
{
temp_sock_descriptor = accept(sock_descriptor, (struct sockaddr*)&pin, &size_of_addr);
printf("[%s, %s, %d]\n", __FILE__, __FUNCTION__, __LINE__);
if(temp_sock_descriptor == -1)
{
perror("accept!");
exit(1);
}
printf("[%s, %s, %d]\n", __FILE__, __FUNCTION__, __LINE__);
if(recv(temp_sock_descriptor, buf, sizeof(buf), 0) == -1)
{
perror("recv()!");
exit(1);
}
else{
printf("[%s, %s, %d]\n", __FILE__, __FUNCTION__, __LINE__);
printf("message received from client: %s\n", buf);
sqlite3 *db = NULL;
char *ErrMsg = 0;
int rc;
char sql[600]={0};
sqlite3_stmt *stat;
rc = sqlite3_open ("mail.db", &db);
if (rc){
fprintf (stderr, "Can't open database: %s\n", sqlite3_errmsg (db));
sqlite3_close (db);
exit (EXIT_FAILURE);
}
else{
printf ("You have opened a sqlite3 database named mail.db successfully!\n");
sqlite3_prepare(db, "INSERT INTO pack(sender,date_time,packname,package) VALUES('Kenber','2012年12月12日12时12分12秒','test.jpg',?);", -1, &stat, 0);
sqlite3_bind_blob(stat, 1, buf, sizeof(buf), NULL);
sqlite3_step(stat);
sqlite3_prepare(db, "select * from pack;", -1, &stat, 0);
sqlite3_step(stat);
const void * test = sqlite3_column_blob(stat, 1);
int size = sqlite3_column_bytes(stat, 1);
sprintf(buf2, "%s", test);
FILE *fp2;
fp2 = fopen("outfile.jpg", "wb");
if(fp2 != NULL)
{
size_t ret = fwrite(test, sizeof(char), size, fp2);
fclose(fp2);
}
}
}
memset(buf, 0, sizeof(buf));
memset(sendmsg,' ',sizeof(send));
strcpy(sendmsg,"message server!");
printf("[%s, %s, %d]\n", __FILE__, __FUNCTION__, __LINE__);
if(send(temp_sock_descriptor, sendmsg, strlen(sendmsg), 0) == -1)
{
perror("send()!");
exit(1);
}
printf("[%s, %s, %d]\n", __FILE__, __FUNCTION__, __LINE__);
close(temp_sock_descriptor);
}
}
client.c:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<arpa/inet.h>
#include<netdb.h>
#include<stddef.h>
char* host_name="127.0.0.1";//"192.168.100.1";
int port=7777;
int main(int argc, char* argv[])
{
char buf[1024];
char message[1024];
int socket_descriptor;
struct sockaddr_in pin;
struct hostent * server_host_name;
char* message_str="client message";
/*if(argc < 2)
{
printf("send default string.\n");
}
else
{
message_str=argv[1];
}
if((server_host_name=gethostbyname(host_name))==0)
{
herror("can not resoving localhost\n");
exit(1);
}*/
bzero(&pin, sizeof(pin));
pin.sin_family=AF_INET;
//pin.sin_addr.s_addr=((struct in_addr*)(server_host_name->h_addr))->s_addr;
pin.sin_addr.s_addr = inet_addr("127.0.0.1");
pin.sin_port=htons(port);
if((socket_descriptor=socket(AF_INET, SOCK_STREAM, 0))==-1)
{
perror("error opening socket\n");
exit(1);
}
if(connect(socket_descriptor, (void*)&pin, sizeof(pin))==-1)
{
perror("can not connecting to server\n");
exit(1);
}
/*if(send(socket_descriptor, message_str, strlen(message_str), 0)==-1)
{
perror("can not send message\n");
exit(1);
}*/
/*********************************传送文件********************************/
FILE *fp;
long filesize = 0;
char * ffile;
size_t sz;
fp = fopen("test.jpg", "rb");
printf("[%s, %s, %d]\n", __FILE__, __FUNCTION__, __LINE__);
if(fp != NULL)
{
printf("[%s, %s, %d]\n", __FILE__, __FUNCTION__, __LINE__);
//计算文件的大小
fseek(fp, 0, SEEK_END);
printf("[%s, %s, %d]\n", __FILE__, __FUNCTION__, __LINE__);
filesize = ftell(fp);
printf("[%s, %s, %d]\n", __FILE__, __FUNCTION__, __LINE__);
fseek(fp, 0, SEEK_SET);
//读取文件
ffile = (char*)malloc(filesize+1);
printf("[%s, %s, %d]\n", __FILE__, __FUNCTION__, __LINE__);
sz = fread(ffile, sizeof(char), filesize+1, fp);
printf("[%s, %s, %d]\n", __FILE__, __FUNCTION__, __LINE__);
fclose(fp);
}
if(send(socket_descriptor, ffile, filesize, 0)<0)
{
printf("[%s, %s, %d]\n", __FILE__, __FUNCTION__, __LINE__);
perror("can not send message\n");
exit(1);
}
else{
printf("[%s, %s, %d]\n", __FILE__, __FUNCTION__, __LINE__);
printf("send file success!\n");
printf("[%s, %s, %d]\n", __FILE__, __FUNCTION__, __LINE__);
}
/*********************************传送文件********************************/
if(recv(socket_descriptor, buf, sizeof(buf), 0)==-1)
{
perror("can not receive response\n");
exit(1);
}
else{
printf("message received from server:%s\n",buf);
}
close(socket_descriptor);
return 0;
}