Linux下 client server 连不上
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Linux下 client server 连不上相关的知识,希望对你有一定的参考价值。
我是菜鸟,在linux下现实先TCP链接,监听什么的函数都没问题,但就是连不上,求解。。我的代码如下。
server:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <ctype.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <arpa/inet.h>
#include <string.h>
#define null 0
#define port 7000
#define backlog 10
void process_server(int s)
ssize_t size = 0;
char buffer[1024];
while(1)
size = read(s, buffer, 1024);
if(size == 0) return;
sprintf(buffer, "%d bytes altogether\n", size);
write(s, buffer, strlen(buffer) + 1);
int main(void)
struct sockaddr_in sin;
struct sockaddr_in cin;
int SS;
int SC;
int addrlen = sizeof(struct sockaddr);
pid_t pid;
int err;
SS = socket(AF_INET, SOCK_STREAM, 0);
if(SS < 0) printf("socket error\n"); return -1;
bzero(&sin, sizeof(sin));
sin.sin_family = AF_INET;
sin.sin_addr.s_addr = htonl(INADDR_ANY);
sin.sin_port = htons(port);
err = bind(SS, (struct sockaddr*)&sin, sizeof(sin));
if(err < 0) perror("bind"); return -1;
listen(SS, backlog);
if(err < 0) printf("listen error\n"); return -1;
printf("wait for the Client...\n");
while(1)
SC = accept(SS, (struct sockaddr*)&cin, &addrlen);
if(SC < 0) continue;
pid = fork();
if(pid == 0)
close(SS);
process_server(SC);
else close(SC);
return 0;
client:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <ctype.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <arpa/inet.h>
#include <string.h>
#define null 0
#define port 7000
#define backlog 10
void process_client(int s)
ssize_t size = 0;
char buffer[1024];
while(1)
size = read(0, buffer, 1024);
if(size > 0)
write(s, buffer, size);
size = read(s, buffer, 1024);
write(1, buffer, size);
int main(int argc, char *argv[])
struct sockaddr_in cin;
int SC;
int err;
close(SC);
SC = socket(AF_INET, SOCK_STREAM, 0);
if(SC < 0) printf("socket error\n"); return -1;
bzero(&cin, sizeof(cin));
cin.sin_family = AF_INET;
//cin.sin_addr.s_addr = htonl(INADDR_ANY);
cin.sin_port = htons(port);
inet_pton(AF_INET, "127.0.0.1", &cin.sin_addr.s_addr);
err = connect(SC, (struct sockaddr*)&cin, sizeof(cin));
if(err < 0) printf("connect error\n"); return -1;
process_client(SC);
close(SC);
return 0;
tcp 0 0 0.0.0.0:7000 0.0.0.0:* LISTEN 4569/ser
tcp 0 0 127.0.0.1:7000 127.0.0.1:33981 ESTABLISHED 4575/ser
tcp 0 0 127.0.0.1:33981 127.0.0.1:7000 ESTABLISHED 4574/cli
这说明客户端和服务端的tcp连接已通。
至于你说的连不上,可能是因为到了启动client后没有任何输入输出提示时你错误的以为连不上。
你这个程序很有问题,server和client会相互阻塞等待(在client终端输入一次后):
server会在read一次后停在“ size = read(s, buffer, 1024);”,
client会停在write一次后“size = read(s, buffer, 1024);”。
因为server在那个while循环里不断等待read,所以process_server函数不会执行到write那步,
同样client因为server端没有给它发送数据而一直阻塞在 process_client函数的read s。
用strace命令可以看出server和client都阻塞在read函数:
server:
strace -p 4575
Process 4575 attached - interrupt to quit
read(4, ^C <unfinished ...>
----------------
client:
strace -p 4574
Process 4574 attached - interrupt to quit
read(3, ^C <unfinished ...> 参考技术A read是阻塞的,比如你server在read的时候,server的这个进程就会阻塞在这里了,你的client也是上来就read这样,他也阻塞在这里了,二个人没有人write全read这样就表现成你代码的效果了。大体给改了一下,client上来发个hello,然后server回一下,client再把这个打印。还有,你server的fork部分原来处理有些问题也做了些修改。修改后代码如下:
server:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <ctype.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <arpa/inet.h>
#include <string.h>
#define null 0
#define port 7000
#define backlog 10
void process_server(int s)
ssize_t size = 0;
char buffer[1024];
// while(1)
size = read(s, buffer, 1024);
if(size == 0) return;
sprintf(buffer, "%d bytes altogether\n", size);
printf("buffer:%s\n",buffer);
write(s, buffer, strlen(buffer) + 1);
int main(void)
struct sockaddr_in sin;
struct sockaddr_in cin;
int SS;
int SC;
int addrlen = sizeof(struct sockaddr);
pid_t pid;
int err;
SS = socket(AF_INET, SOCK_STREAM, 0);
if(SS < 0) printf("socket error\n"); return -1;
bzero(&sin, sizeof(sin));
sin.sin_family = AF_INET;
sin.sin_addr.s_addr = htonl(INADDR_ANY);
sin.sin_port = htons(port);
err = bind(SS, (struct sockaddr*)&sin, sizeof(sin));
if(err < 0) perror("bind"); return -1;
listen(SS, backlog);
if(err < 0) printf("listen error\n"); return -1;
printf("wait for the Client...\n");
while(1)
SC = accept(SS, (struct sockaddr*)&cin, &addrlen);
if(SC < 0) continue;
pid = fork();
if(pid > 0)
//父进程继续监听
close(SC);
else if (pid == 0) //子进程处理这样写容易产生僵死进程
process_server(SC);
close(SC);
else //error
close(SC);
return 0;
client:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <ctype.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <arpa/inet.h>
#include <string.h>
#define null 0
#define port 7000
#define backlog 10
void process_client(int s)
ssize_t size = 0;
char buffer[1024];
// while(1)
// size = read(0, buffer, 1024);
// if(size > 0)
// write(s, buffer, size);
write(s,"hello", sizeof("hello"));
printf("write over\n");
size = read(s, buffer, 1024);
printf("buffer:%s\n",buffer);
// write(1, buffer, size);
int main(int argc, char *argv[])
struct sockaddr_in cin;
int SC;
int err;
close(SC);
SC = socket(AF_INET, SOCK_STREAM, 0);
if(SC < 0) printf("socket error\n"); return -1;
bzero(&cin, sizeof(cin));
cin.sin_family = AF_INET;
//cin.sin_addr.s_addr = htonl(INADDR_ANY);
cin.sin_port = htons(port);
inet_pton(AF_INET,"127.0.0.1", &cin.sin_addr.s_addr);
err = connect(SC, (struct sockaddr*)&cin, sizeof(cin));
if(err < 0) printf("connect error\n"); return -1;
printf("enter here\n");
process_client(SC);
close(SC);
return 0;
参考技术B 主机的防火墙是必须关的。
主机上使用软件连接登陆VMWare上的Linux,有两种方式可以连接:
(1)桥接方式: 使主机与Linux的IP在同一个网段中,就可以了,这样主机必须要插上网线。
(2)NAT方式:这种主机不需要插网线,在主机上查找VMnet8的IP,然后在Linux中设置IP,Linux的IP与VMnet8的IP在同一个网段中,Linux的网关和DNS都是VMnet8的IP。设置完成之后,将Linux的网卡解除再激活,就行了。
vSphere Client连不上VCenter,VCenter已启动,linux版本。
vSphere Client无法连接到“100.100.88.30”(这是VCenter的网址,能ping通)。出现未知连接错误。(服务器无法解释客户端的请求。(远程服务器返回错误:(503)服务器不可用。))
在浏览器里输入http://100.100.88.30:5480,看看服务是不是都已经正常启动,如果没有,也不用查错了,重新部署一次vcenter linux版,比查错的时间短多了如果服务一切正常,那么确认一下你的client的版本不能低于vcenter的版本 参考技术A vsphere 是什么版本的?以前可以吗
以上是关于Linux下 client server 连不上的主要内容,如果未能解决你的问题,请参考以下文章
vSphere Client连不上VCenter,VCenter已启动,linux版本。
putty连不上linux,提示:Network error:Connection refused
Genymotion 模拟器连接不上(adb server version (40) doesn't match this client (39))