当前位置: 技术问答>linux和unix
一个很简单的通过socket发送接受文件的程序,出现了segmentation fault错误。请各位帮忙
来源: 互联网 发布时间:2015-10-14
本文导语: 第一次写程序,大体过程如下: 客户端程序,建立socket——〉配置服务器端口和地址等-〉connect -> send 一个*.xml文件 之后接受服务器端发送的另一个xml文件,保存,结束。 #include #include #include...
第一次写程序,大体过程如下:
客户端程序,建立socket——〉配置服务器端口和地址等-〉connect -> send 一个*.xml文件
之后接受服务器端发送的另一个xml文件,保存,结束。
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define SERVPORT 8080
main (int argc,char *argv[]) {
int sockfd;
long recvbytes,filesize;
char filename[] = "request.xml";
char *buf_file;
FILE *req, *res;
struct sockaddr_in serv_addr;
long GetFileSize(char fn[]);
/* configure socket, such as server ip address and port */
if ((sockfd = socket(AF_INET,SOCK_STREAM,0)) == -1) {
perror(" socket failed. ");
exit(1);
}
serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons(SERVPORT);
if ((serv_addr.sin_addr.s_addr = inet_addr("172.25.21.41")) == -1) {
perror("Server IP address failed.");
exit(1);
}
bzero(&(serv_addr.sin_zero),8);
/*open file, read it in memory*/
if ((req = fopen(filename,"r")) == NULL) {
perror("open file failed.");
exit(1);
}
filesize = GetFileSize(filename);
buf_file = malloc(filesize+1);
fread(buf_file,filesize,1,req);
fclose(req);
/*connect to server,send file*/
if (connect(sockfd, (struct sockaddr *)&serv_addr, sizeof(struct sockaddr)) == -1) {
perror("connect failed.");
exit(1);
}
if (send (sockfd,buf_file,filesize,0) == -1) {
perror("send failed.");
exit(1);
}
/* recieve the file which server send*/
if ((recvbytes = recv(sockfd, buf_file,filesize,0)) == -1) {
perror("recieve failed.");
exit(1);
}
/*write in file*/
if ((res = fopen("response.xml","w+")) == NULL) {
perror("open file failed.");
exit(1);
}
fwrite(buf_file,filesize,1,res);
fclose(res);
free(buf_file);
return 0;
}
long GetFileSize(char fn[]) {
struct stat *buf;
if (stat (fn, buf) == -1) {
perror("Get statics of file failed.");
exit(1);
}
return buf->st_size;
}
谢谢各位帮忙。下班前结账。谢谢了。
客户端程序,建立socket——〉配置服务器端口和地址等-〉connect -> send 一个*.xml文件
之后接受服务器端发送的另一个xml文件,保存,结束。
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define SERVPORT 8080
main (int argc,char *argv[]) {
int sockfd;
long recvbytes,filesize;
char filename[] = "request.xml";
char *buf_file;
FILE *req, *res;
struct sockaddr_in serv_addr;
long GetFileSize(char fn[]);
/* configure socket, such as server ip address and port */
if ((sockfd = socket(AF_INET,SOCK_STREAM,0)) == -1) {
perror(" socket failed. ");
exit(1);
}
serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons(SERVPORT);
if ((serv_addr.sin_addr.s_addr = inet_addr("172.25.21.41")) == -1) {
perror("Server IP address failed.");
exit(1);
}
bzero(&(serv_addr.sin_zero),8);
/*open file, read it in memory*/
if ((req = fopen(filename,"r")) == NULL) {
perror("open file failed.");
exit(1);
}
filesize = GetFileSize(filename);
buf_file = malloc(filesize+1);
fread(buf_file,filesize,1,req);
fclose(req);
/*connect to server,send file*/
if (connect(sockfd, (struct sockaddr *)&serv_addr, sizeof(struct sockaddr)) == -1) {
perror("connect failed.");
exit(1);
}
if (send (sockfd,buf_file,filesize,0) == -1) {
perror("send failed.");
exit(1);
}
/* recieve the file which server send*/
if ((recvbytes = recv(sockfd, buf_file,filesize,0)) == -1) {
perror("recieve failed.");
exit(1);
}
/*write in file*/
if ((res = fopen("response.xml","w+")) == NULL) {
perror("open file failed.");
exit(1);
}
fwrite(buf_file,filesize,1,res);
fclose(res);
free(buf_file);
return 0;
}
long GetFileSize(char fn[]) {
struct stat *buf;
if (stat (fn, buf) == -1) {
perror("Get statics of file failed.");
exit(1);
}
return buf->st_size;
}
谢谢各位帮忙。下班前结账。谢谢了。
|
long GetFileSize(char fn[]) {
struct stat *buf; //这里
if (stat (fn, buf) == -1) {
perror("Get statics of file failed.");
exit(1);
}
return buf->st_size;
}
struct stat *buf; //这里
if (stat (fn, buf) == -1) {
perror("Get statics of file failed.");
exit(1);
}
return buf->st_size;
}
|
同意楼上,没有分配内存,修改为
long GetFileSize(char fn[]) {
struct stat buf;
if (stat (fn, &buf) == -1) {
perror("Get statics of file failed.");
exit(1);
}
return buf.st_size;
}
long GetFileSize(char fn[]) {
struct stat buf;
if (stat (fn, &buf) == -1) {
perror("Get statics of file failed.");
exit(1);
}
return buf.st_size;
}