L
lamez
Самая простая прога клиент и серве. Запускаю серв, потом клиент. пишет Connection refused в errno после комманды connnect в клиенте.
Сервер:
[codebox]#include <sys/types.h>
#include <sys/socket.h>
#include <stdio.h>
#include <netinet/in.h>
#include <iostream>
using namespace std;
int main(void){
sockaddr_in connection_struct;
int sock, receiver, acceptor;
char buf[1];
bzero(&connection_struct, sizeof(connection_struct));
connection_struct.sin_len = sizeof(connection_struct);
connection_struct.sin_family = AF_INET;
connection_struct.sin_port = (135);
connection_struct.sin_addr.s_addr = htonl(INADDR_ANY);
sock = socket(AF_INET, SOCK_STREAM, 0);
if(sock<0){
cerr<<"unable to create socket"<<endl;
exit(1);
}
cout<<"now bind"<<endl;
receiver = bind(sock, (struct sockaddr*)&connection_struct, sizeof(connection_struct));
if(receiver < 0){
cerr<<"unable to bind"<<endl;
exit(1);
}
cout<<"binded\t"<<endl;
receiver = listen(sock, 15);
if(receiver){
cerr<<"unable to listen"<<endl;
exit(1);
}
cout<<"listen\t"<<endl<<endl;
if((acceptor = accept(sock, NULL, NULL)) < 0){
cerr<<"unable to accept here is *цензура* off"<<endl;
exit(1);
}
cout<<"accept\t"<<endl<<endl;
receiver = recv(acceptor, buf, 1, 0);
if(receiver < 1){
cerr<<"unable to receive"<<endl;
exit(1);
}
cout<<"receive\t"<<endl;
printf("%cn", buf[0]);
receiver = send(acceptor, "2", 1,0);
cout<<"send\t";
if(receiver<1)
cerr<<"cant send"<<endl;
exit(0);
}
[/codebox]
Клиент:
[codebox]#include <iostream>
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/uio.h>
#include <errno.h>
using namespace std;
int main(void){
sockaddr_in connection_struct;
int sock;
int connected_sock;
char buf[1];
bzero(&connection_struct, sizeof(connection_struct));
connection_struct.sin_len = sizeof(connection_struct);
connection_struct.sin_family = AF_INET;
connection_struct.sin_port = htons(135);
connection_struct.sin_addr.s_addr = inet_addr("127.0.0.1");
sock = socket(AF_INET, SOCK_STREAM, 0);
if(sock < 0){
cerr<<"ошибка вызова socket"<<endl;
exit(1);
}
connected_sock = connect(sock, (struct sockaddr *)&connection_struct, sizeof(connection_struct));
if(connected_sock < 0){
cerr<<"unable to connect "<< connected_sock <<endl;
cout<<strerror(errno)<<endl;
exit(1);
}
connected_sock = send(sock, "1", 1, 0);
if(connected_sock<1){
cerr<<"cant setd it *цензура*"<<endl;
exit(1);
}
connected_sock = recv(sock, buf, 1, 0);
if(connected_sock<1){
cerr<<"unable to recieve buf"<<endl;
exit(1);
}
else
printf(" % cn", buf[0]);
exit(0);
}
[/codebox]
Порты разные пробовал, не помогает
Сервер:
[codebox]#include <sys/types.h>
#include <sys/socket.h>
#include <stdio.h>
#include <netinet/in.h>
#include <iostream>
using namespace std;
int main(void){
sockaddr_in connection_struct;
int sock, receiver, acceptor;
char buf[1];
bzero(&connection_struct, sizeof(connection_struct));
connection_struct.sin_len = sizeof(connection_struct);
connection_struct.sin_family = AF_INET;
connection_struct.sin_port = (135);
connection_struct.sin_addr.s_addr = htonl(INADDR_ANY);
sock = socket(AF_INET, SOCK_STREAM, 0);
if(sock<0){
cerr<<"unable to create socket"<<endl;
exit(1);
}
cout<<"now bind"<<endl;
receiver = bind(sock, (struct sockaddr*)&connection_struct, sizeof(connection_struct));
if(receiver < 0){
cerr<<"unable to bind"<<endl;
exit(1);
}
cout<<"binded\t"<<endl;
receiver = listen(sock, 15);
if(receiver){
cerr<<"unable to listen"<<endl;
exit(1);
}
cout<<"listen\t"<<endl<<endl;
if((acceptor = accept(sock, NULL, NULL)) < 0){
cerr<<"unable to accept here is *цензура* off"<<endl;
exit(1);
}
cout<<"accept\t"<<endl<<endl;
receiver = recv(acceptor, buf, 1, 0);
if(receiver < 1){
cerr<<"unable to receive"<<endl;
exit(1);
}
cout<<"receive\t"<<endl;
printf("%cn", buf[0]);
receiver = send(acceptor, "2", 1,0);
cout<<"send\t";
if(receiver<1)
cerr<<"cant send"<<endl;
exit(0);
}
[/codebox]
Клиент:
[codebox]#include <iostream>
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/uio.h>
#include <errno.h>
using namespace std;
int main(void){
sockaddr_in connection_struct;
int sock;
int connected_sock;
char buf[1];
bzero(&connection_struct, sizeof(connection_struct));
connection_struct.sin_len = sizeof(connection_struct);
connection_struct.sin_family = AF_INET;
connection_struct.sin_port = htons(135);
connection_struct.sin_addr.s_addr = inet_addr("127.0.0.1");
sock = socket(AF_INET, SOCK_STREAM, 0);
if(sock < 0){
cerr<<"ошибка вызова socket"<<endl;
exit(1);
}
connected_sock = connect(sock, (struct sockaddr *)&connection_struct, sizeof(connection_struct));
if(connected_sock < 0){
cerr<<"unable to connect "<< connected_sock <<endl;
cout<<strerror(errno)<<endl;
exit(1);
}
connected_sock = send(sock, "1", 1, 0);
if(connected_sock<1){
cerr<<"cant setd it *цензура*"<<endl;
exit(1);
}
connected_sock = recv(sock, buf, 1, 0);
if(connected_sock<1){
cerr<<"unable to recieve buf"<<endl;
exit(1);
}
else
printf(" % cn", buf[0]);
exit(0);
}
[/codebox]
Порты разные пробовал, не помогает