#include <arpa/inet.h>
#include <netdb.h>
#include <netinet/in.h> // for sockaddr_in
#include <sys/types.h> // for socket
#include <sys/socket.h> // for socket
#include <stdio.h> // for printf
#include <stdlib.h> // for exit
#include <string.h> // for bzero
#include <time.h> //for time_t and time
#define CLIENT_PORT 1234
#define MAX 1024
char *IP="127.0.0.1";
int client_socket;
int ret;
int my;
int main()
{
client_socket =socket(AF_INET,SOCK_STREAM,0);
if(-1 == client_socket)
{
perror("sosck failed\n");
exit(-1);
}
struct sockaddr_in client_addr;
client_addr.sin_family =AF_INET;
client_addr.sin_port =htons(CLIENT_PORT);
client_addr.sin_addr.s_addr = inet_addr(IP);
ret =connetct(client_socket, (struct sockaddr *)&client_addr, sizeof(struct sockaddr));
if(-1 == ret)
{
perror("connetct failed\n");
exit(-1);
}
while(1)
{
FILE *stream;
char buf[MAX]="hello 服务器";
strcat(buf,"发送请求");
if(-1 == send(client_socket,buf,sizeof(buf)+1,0))
{
perror("send failed\n");
exit(-1);
}
ret =recv(client_socket,buf,1024,0);
if(-1 ==ret)
{
perror("recv failed\n");
exit(-1);
}
if(fopen("file2","w+") == NULL)
{
printf("the file open error\n");
exit(-1);
}
else
{
printf("the file open successful\n");
}
printf("Send %s\n",buf);
int lengsize = 0;
if(lengsize=fwrite(buf,1,1024,stream) == -1)
{
printf("lengsize = %d\n",lengsize);
if(send(client_socket,buf,lengsize,0)<0)
{
printf("Send File is Failed\n");
break;
}
bzero(buf, 1024);
if(fclose(stream))
{
printf("The file 'data' was not closed! \n");
exit(1);
}
}
close(client_socket);
return 0;
}
你用的什么编辑器写代码?{}不配对就不给你提示吗?回复<aclass='referer'target='_blank'>@tb_engineer:你说的很对,细心真的很重要,对程序员来说,谢谢了,万分感谢。回复<aclass='referer'target='_blank'>@DavidWTF:没有粘了一个}回复<aclass='referer'target='_blank'>@tb_engineer:那就不知道了,你帖的代码上一共11个{,10个},不信自己数数。细心是程序员的第一要求.回复<aclass='referer'target='_blank'>@DavidWTF:不对,不是少}的问题,回复<aclass='referer'target='_blank'>@tb_engineer:怪不得,最后少个}
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。