当前位置: 技术问答>linux和unix
socket编程 哪位高手帮我看看下面的问题,为什么输出结果会这样,帮我解释解释
来源: 互联网 发布时间:2017-02-07
本文导语: 我只在客户端输入一个a,然后会车 [root@heiying have_test]# ./server_thread com_fd=4 new_fd=4 Message from client: f Message from client: [root@heiying have_test]# ./client please input a chars f snd_buf = f Message form server: a please input a ...
我只在客户端输入一个a,然后会车
[root@heiying have_test]# ./server_thread
com_fd=4
new_fd=4
Message from client: f
Message from client:
[root@heiying have_test]# ./client
please input a chars
f
snd_buf = f
Message form server: a
please input a chars
snd_buf =
Message form server: a
please input a chars
服务端:
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define PORT 8888
//线程执行函数负责读写
void *thr_fn(void *arg)
{
int size,j;
//char recv_buf[100];
char recv_buf;
int *parg=(int *)arg;
int new_fd=*parg;
printf("new_fd=%dn",new_fd);
while(1)
{
read(new_fd,&recv_buf, sizeof(recv_buf));
printf("Message from client: %cn",recv_buf);
recv_buf = 'a';
write(new_fd, &recv_buf, sizeof(recv_buf));
}
close(new_fd);
return 0;
}
int main(int argc,char *argv[])
{
socklen_t clt_addr_len;
int listen_fd;
int com_fd;
int ret;
int i;
int len;
//static char recv_buf;
pthread_t tid;
struct sockaddr_in clt_addr;
struct sockaddr_in srv_addr;
//创建套接字用于服务器的监听
listen_fd=socket(PF_INET,SOCK_STREAM,0);
if(listen_fd
[root@heiying have_test]# ./server_thread
com_fd=4
new_fd=4
Message from client: f
Message from client:
[root@heiying have_test]# ./client
please input a chars
f
snd_buf = f
Message form server: a
please input a chars
snd_buf =
Message form server: a
please input a chars
服务端:
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define PORT 8888
//线程执行函数负责读写
void *thr_fn(void *arg)
{
int size,j;
//char recv_buf[100];
char recv_buf;
int *parg=(int *)arg;
int new_fd=*parg;
printf("new_fd=%dn",new_fd);
while(1)
{
read(new_fd,&recv_buf, sizeof(recv_buf));
printf("Message from client: %cn",recv_buf);
recv_buf = 'a';
write(new_fd, &recv_buf, sizeof(recv_buf));
}
close(new_fd);
return 0;
}
int main(int argc,char *argv[])
{
socklen_t clt_addr_len;
int listen_fd;
int com_fd;
int ret;
int i;
int len;
//static char recv_buf;
pthread_t tid;
struct sockaddr_in clt_addr;
struct sockaddr_in srv_addr;
//创建套接字用于服务器的监听
listen_fd=socket(PF_INET,SOCK_STREAM,0);
if(listen_fd