Windows 10 da bir c++ soket programı yazmaya çalışıyorum, server.cpp ve client.cpp çalışıyor ancak,ikisi arasında bağ kuramıyorum hata kodu olarak 10047 hata kodunu vermekte,kodumu göstereyim;
//SERVER
#include<iostream>
#include<winsock2.h>
#include<windows.h>
#include<ws2tcpip.h>
int main(){
WSADATA wsaData;
if (WSAStartup(MAKEWORD(2,2), &wsaData) != 0) {
std::cout<<"WSAStartup hatasi."<<std::endl;
exit(1);
}
std::cout<<"--------------------SERVER------------------"<<std::endl;
SOCKET sock;
sock=socket(AF_INET,SOCK_STREAM,0);
if(sock==INVALID_SOCKET){
std::cout<<"Socket hatasi."<<std::endl;
}
struct sockaddr_in adres;
adres.sin_addr.s_addr=INADDR_ANY;
adres.sin_family=AF_INET;
adres.sin_port=htons(5055);
int a;
a=bind(sock,(struct sockaddr*)&adres,sizeof(adres));
if(a==-1){
std::cout<<"Hata //bind//:"<<WSAGetLastError()<<std::endl;
}
int l=listen(sock,5);
if(l==-1){
std::cout<<"Listen hatasi."<<std::endl;
}
//cONNECTYON BEKLEME;
struct sockaddr_in client;
int adres_size=sizeof(client);
SOCKET client_socket=accept(sock,(struct sockaddr*)&client,&adres_size);
char message_from_client[1024];
int r;
closesocket(sock);
while(1){
if(client_socket!=-1){
r=recv(sock,message_from_client,1024,0);
if(r!=-1){
std::cout<<"Client:"<<message_from_client;
}
}
else{
// std::cout<<"Baglanan birisi var ancak pm atmiyor."<<std::endl;
}
}
closesocket(sock);
WSACleanup();
return 0;
}
//CLİENT
#include<iostream>
#include<winsock2.h>
#include<windows.h>
#include<ws2tcpip.h>
#include<string.h>
#include<string>
#pragma comment (lib,"ws2_32.lib")
int main(){
WSADATA wsaData;
if (WSAStartup(MAKEWORD(2,2), &wsaData) != 0) {
std::cout<<"WSAStartup hatasi"<<std::endl;
exit(1);
}
std::cout<<"--------------------CLIENT------------------"<<std::endl;
SOCKET cl_sock;
cl_sock=socket(AF_INET,SOCK_STREAM,0);
if(cl_sock==INVALID_SOCKET){
std::cout<<"Socket hatasi."<<std::endl;
}
struct sockaddr_in cl_adres;
cl_adres.sin_addr.s_addr=INADDR_ANY;
cl_adres.sin_family=AF_INET;
cl_adres.sin_port=htons(5055);
char message[1024];
struct sockaddr_in server_adres;
while(1){
SOCKET c=connect(cl_sock,(struct sockaddr*)&server_adres,sizeof(server_adres));
if(c!=-1){
std::cout<<"Mesaj:";
std::cin>>message[1024];
send(cl_sock,&message[1024],1024+1,0);
}
else{
std::cout<<"Serverle baglanti kurulamadi."<<std::endl;
Sleep(750);
std::cout<<WSAGetLastError();
}
}
closesocket(cl_sock);
WSACleanup();
return 0;
}