is it possible to make a connection doing something like i've done
in my code.
a copy of the program is ment to be used on another computer
ive tried executing 2 copies and tested it on my own machine.
the problem is that it detects the other user but u cant send and receive anything.
i dont know if its a miss ive done in the code or its something with my computer or localserver.
can someone with a good knowledge in networking explain to me what is wrong
thanks for your help!Code:#include <string.h> #include <iostream.h> #pragma comment (lib,"wsock32.lib") #include <winsock.h> #define PORT 7777 int main() { char *buf="MESSAGE"; char *rec=new char[256]; WORD sockVersion; WSADATA wsaData; sockVersion = MAKEWORD(2,0); WSAStartup(sockVersion, &wsaData); SOCKET sock1, sock2; sock1=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP); HOSTENT *host; host=gethostbyname("localhost"); SOCKADDR_IN in_s, ut_s; in_s.sin_family=AF_INET; in_s.sin_addr.s_addr=0; in_s.sin_port=htons(PORT); ut_s.sin_family=AF_INET; ut_s.sin_port=htons(PORT); ut_s.sin_addr=*((LPIN_ADDR)*host->h_addr_list); int b=bind(sock1,(const struct sockaddr*)&in_s,sizeof(struct sockaddr)); if(b==INVALID_SOCKET) { cout<<"error bind()"; } int l=listen(sock1,10); if(l==SOCKET_ERROR) { cout<<"error listen()"; } int size=sizeof(struct sockaddr_in); sock2=accept(sock1,(struct sockaddr*)&ut_s,&size); if(sock2==SOCKET_ERROR) { cout<<"error accept()"; } int s=send(sock2,buf,strlen(buf),0); if(s==SOCKET_ERROR) { cout<<"error send()"; } int r=recv(sock2,rec,sizeof(rec),0); if(r==SOCKET_ERROR) { cout<<"error recv()"; } int c=connect(sock1,(struct sockaddr*)&ut_s,sizeof(struct sockaddr)); if(c==SOCKET_ERROR) { cout<<"error connect()"; } closesocket(sock1); closesocket(sock2); WSACleanup(); return 0; }



LinkBack URL
About LinkBacks



