简单linux socket 编程,客户端连接不能断开。不好意思自己太大意了。。
时间:2010-07-02
来源:互联网
本帖最后由 peijue 于 2010-07-02 18:51 编辑
server.c
复制代码
client.c
复制代码
初学linux下的编程,可能是线程的问题, 如果文件名正确,服务器上有这个文件就可以成功完成,要是文件不存在的话,也能发送消息回去(就是能发送错误信息给客户端, 客户端也能显示),可是为何客户的连接不能断开呢??
希望各位帮帮忙。。
server.c
- #include <sys/types.h>
- #include <pthread.h>
- #include <sys/socket.h>
- #include <netdb.h>
- #include <stdlib.h>
- #include <stdio.h>
- #include <netinet/in.h>
- #include <fcntl.h>
- #define SERVER_PORT 48888 /*定义通信端口 */
- #define BUF_SIZE 4096 /*定义传输数据的大小*/
- #define QUEUE_SIZE 10 /* 定义最大连接数 */
-
- void *work_thread(void *c)
- {
- int sockfd = *(int*)c;
- printf("thread id : %u\n", pthread_self() );
- int fd, l;
- char buf[BUF_SIZE];
- l = recv(sockfd, buf, BUF_SIZE, 0);
- printf("%s\n", buf);
- if( l == -1) perror("l");
- fd = open(buf, O_RDONLY);
- if( fd < 0 )
- {
- printf("open file fail: %s\n", buf);
- sprintf(buf, "%s", "not the file in server\n");
- send(sockfd, buf, strlen(buf)+1, 0);
- [color=Red]close(sockfd); /* 忘了加这句,非常对不起,是我太大意了,没有关闭连接,对不起各位*/[/color]
- return (void *)1;
- }
- while(1)
- {
- l = read(fd, buf, BUF_SIZE);
- if( l <= 0 ) break;
- send(sockfd, buf, l, 0);
- }
- printf("send file success\n");
- close(fd);
- close(sockfd);
- return (void *)1;
- }
-
- int main( int argc, char *argv[] )
- {
- int b, s, l, sa, fd, bytes, on = 1;
- char buf[ BUF_SIZE ]; /*发送文件的缓冲区*/
- struct sockaddr_in channel, addr_c; /* 控制IP 地址的结构 */
-
- /*--初始化套接字地址结构--*/
- memset(&channel, 0, sizeof(struct sockaddr_in ) );
- channel.sin_family = AF_INET;
- channel.sin_addr.s_addr = htonl( INADDR_ANY );
- channel.sin_port = htons( SERVER_PORT );
-
-
- /*--创建套接字等待连接--*/
-
- s = socket( AF_INET, SOCK_STREAM, IPPROTO_TCP );
- if( s < 0 ) perror("s");
- setsockopt( s, SOL_SOCKET, SO_REUSEADDR, (char *)&on, sizeof(on) );
-
- b = bind( s, (struct sockaddr *)&channel, sizeof(channel) );
- if( b < 0) perror("b");
-
- l = listen( s, QUEUE_SIZE );
- int len = sizeof(struct sockaddr_in);
- if( l < 0) perror("l");
- /* 等待连接到来,并处理连接请求*/
- while(1)
- {
- int err;
- sa = accept( s, (struct sockaddr *)&addr_c, &len );/*接受一个套接字的请求*/
- if( sa < 0)
- perror("sa");
- pthread_t tid;
- err = pthread_create(&tid, NULL, work_thread, &sa);
- if(err != 0)
- {
- printf("create thread failed");
- exit(1);
- }
- }
-
- }
- #include <sys/types.h>
- #include <sys/socket.h>
- #include <netinet/in.h>
- #include <netdb.h>
- #include <stdio.h>
- #include <stdlib.h>
-
- #define SERVER_PORT 48888
- #define BUF_SIZE 4096
-
- int main(int argc, char *argv[] )
- {
- int c, s, bytes;
- char buf[BUF_SIZE];
- struct hostent *h;
- struct sockaddr_in channel;
-
- if( argc != 3 ) perror("argc");
- h = gethostbyname(argv[1]);
-
- if( !h ) perror("gethostbyname failed");
-
-
- s = socket( PF_INET, SOCK_STREAM, IPPROTO_TCP );
- if(s<0) perror("socket");
-
- memset( &channel, 0, sizeof(channel) );
- channel.sin_family = AF_INET;
- memcpy(&channel.sin_addr.s_addr, h->h_addr, h->h_length );
- channel.sin_port = htons(SERVER_PORT);
-
- c = connect(s, (struct sockaddr *)&channel , sizeof(channel));
- if(c < 0) perror("connect failed");
-
- write(s, argv[2], strlen(argv[2])+1 );
-
-
- while(1)
- {
- bytes = read( s, buf, BUF_SIZE );
- if( bytes <= 0 ) break;
- write(1, buf, bytes);
- }
- }
希望各位帮帮忙。。
作者: peijue 发布时间: 2010-07-02
补充一点,客户端 有两个参数,一个是IP,一个是文件名。
为何,ubuntu下 chrome 的字体比firefox难看呢
为何,ubuntu下 chrome 的字体比firefox难看呢

作者: peijue 发布时间: 2010-07-02
没看明白 要问什么
作者: ssuclinux 发布时间: 2010-07-02
就是说客户端如果输入不存在的文件名,会收到一个错误信息,可是客户端进程不会退出。
作者: peijue 发布时间: 2010-07-02
你客户端程序 判断退出的条件是 收到 负数的时候,也就是socket关闭才会退出,
你服务器没有关闭socket, 而且给客户端发信息了,客户端能接收,bytes是不会 < 0 的,
你的条件不成立,肯定不会退出
你服务器没有关闭socket, 而且给客户端发信息了,客户端能接收,bytes是不会 < 0 的,
你的条件不成立,肯定不会退出
作者: ssuclinux 发布时间: 2010-07-02
回复 ssuclinux
恩 , 是这样的, 我没有关闭连接,不好意思。太大意了。。谢谢你。
恩 , 是这样的, 我没有关闭连接,不好意思。太大意了。。谢谢你。
作者: peijue 发布时间: 2010-07-02
相关阅读 更多
热门阅读
-
office 2019专业增强版最新2021版激活秘钥/序列号/激活码推荐 附激活工具
阅读:74
-
如何安装mysql8.0
阅读:31
-
Word快速设置标题样式步骤详解
阅读:28
-
20+道必知必会的Vue面试题(附答案解析)
阅读:37
-
HTML如何制作表单
阅读:22
-
百词斩可以改天数吗?当然可以,4个步骤轻松修改天数!
阅读:31
-
ET文件格式和XLS格式文件之间如何转化?
阅读:24
-
react和vue的区别及优缺点是什么
阅读:121
-
支付宝人脸识别如何关闭?
阅读:21
-
腾讯微云怎么修改照片或视频备份路径?
阅读:28