Network C/C++ ile soket programlama

xpression

Yeni üye
13 Ocak 2019
1
0
İyi günler bir süredir TCP/IP protokolü ile iki windows işletim sistemine sahip bilgisayar arasında veri iletişimi yapmak için uğraşıyorum.
Kendi bilgisayarım üzerinde TCP server ve Client çalıştırdığımda herhangi bir sorun olmazken iki ayrı ve aynı ağa bağlı bilgisayarlar üzerinde birisi Server diğeri Client olmak üzere deneme yaparken sonuç alamıyorum

Kod:
//TCP SERVER
#include<WinSock2.h>
#include<iostream>
using namespace std;
int main()
{
 cout<<"\t\t------ TCP SERVER ---------"<<endl;
//Local Variable
 WSADATA   Winsockdata ;
 int       iWsaStartup ;
 int       iWsaCleanup;

 SOCKET   TCPServerSocket;
 int      iCloseSocket;

 struct  sockaddr_in  TCPServerAdd;
 struct  sockaddr_in  TCPClientAdd;
 int     iTCPClientAdd    = sizeof(TCPClientAdd);

 int iBind ;

 int iListen;

 SOCKET sAcceptSocket;

 int   iSend;
 char  SenderBuffer[512] = "Hello from Server!";
 int   iSenderBuffer = strlen( SenderBuffer )+1;

 int  iRecv;
 char RecvBuffer[512];
 int  iRecvBuffer = 512;

// STEP -1 WSAStartUp Fun
iWsaStartup = WSAStartup(MAKEWORD(2,2),&Winsockdata);
if ( iWsaStartup!= 0 )
{
cout<<"WSAStartUp Failed"<<endl;
}
cout<<"WSAStartUp Success"<<endl;

// STEP-2 Fill the Structure
TCPServerAdd.sin_family = AF_INET;
TCPServerAdd.sin_addr.s_addr =inet_addr("127.0.0.1");//htonl(INADDR_ANY);
TCPServerAdd.sin_port = htons(8080);

//STEP -3 Socket Creation
TCPServerSocket = socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
if (TCPServerSocket == INVALID_SOCKET)
{
cout<<"TCP Server Socket Creation Failed"<<endl;
cout<<"Error Code - "<<WSAGetLastError()<<endl;
}
cout<<"TCP Server Socket Creation Success"<<endl;

//STEP-4 bind fun
 iBind = bind(
        TCPServerSocket,
        (SOCKADDR*)&TCPServerAdd,
        sizeof(TCPServerAdd));
if (iBind == SOCKET_ERROR)
{
cout<<"Binding Failed"<<endl;
cout<<"Error No-> "<<WSAGetLastError()<<endl;
}
cout<<"Binding Success"<<endl;

//STEP-5 Listen fun
iListen = listen(TCPServerSocket,2);
if (iListen == SOCKET_ERROR)
{
cout<<"Listen Fun Failed "<<endl;
cout<<"Error No-> "<<WSAGetLastError()<<endl;
}
cout<<"Listen Fun Success"<<endl;

// STEP-6 Accept
sAcceptSocket = accept(
               TCPServerSocket,
               (SOCKADDR*)&TCPClientAdd,
               &iTCPClientAdd);
if (sAcceptSocket == INVALID_SOCKET)
{
cout<<"Accept Failed "<<endl;
cout<<"Error No-> "<<WSAGetLastError()<<endl;
}
cout<<"Connection Accepted"<<endl;

// STEP-7 Send Data to Client
iSend =send(sAcceptSocket,SenderBuffer,iSenderBuffer,0);
if (iSend == SOCKET_ERROR)
{
cout<<"Sending Failed "<<endl;
cout<<"Error No-> "<<WSAGetLastError()<<endl;
}
cout<<"Data Sending Success "<<endl;

// STEP-8 Recv Data from Client
iRecv = recv(sAcceptSocket,RecvBuffer,iRecvBuffer,0);
if (iRecv == SOCKET_ERROR)
{
cout<<"Receive Data Failed "<<endl;
cout<<"Error No-> "<<WSAGetLastError()<<endl;
}
cout<<"DATA RECEIVED -> "<<RecvBuffer<<endl;
memset(&RecvBuffer[0], 0, sizeof(RecvBuffer));
// STEP-9 Close Socket
iCloseSocket = closesocket(TCPServerSocket);
if (iCloseSocket == SOCKET_ERROR)
{
cout<<"Closing Socket Failed "<<endl;
cout<<"Error No-> "<<WSAGetLastError()<<endl;
}
cout<<"Closing Socket Success"<<endl;
// STEP-10 CleanUp from DLL
iWsaCleanup = WSACleanup();
if (iWsaCleanup == SOCKET_ERROR)
{
cout<<"CleanUp Fun Failed "<<WSAGetLastError()<<endl;
cout<<"Error No-> "<<WSAGetLastError()<<endl;
}
cout<<"CleanUp Fun Success"<<endl;

system("PAUSE");
return 0;
}

Kod:
//TCP CLIENT
#include<WinSock2.h>
#include<iostream>
#include<stdio.h>
using namespace std;
int main()
{
cout<<"\t\t------ TCP CLIENT --------"<<endl;
//Local Variable
WSADATA       WinSockData;
int           iWsaStartup ;
int           iWsaCleanup ;

SOCKET        TCPClientSocket;
int           iCloseSocket;

struct   sockaddr_in     TCPServerAdd;

int           iConnect;

int           iRecv;
char          RecvBuffer[512];
int           iRecvBuffer = 512;

int        iSend;
char       SenderBuffer[512] = "Hello from Client";
int        iSenderBuffer = strlen( SenderBuffer )+1;

//STEP-1 WSASatrtUp Fun
iWsaStartup = WSAStartup(MAKEWORD(2,2),&WinSockData);
if ( iWsaStartup!= 0 )
{
cout<<"WSAStartUp Failed"<<endl;
}
cout<<"WSAStartUp Success"<<endl;
// STEP-2 Socket Creation
TCPClientSocket = socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
if (TCPClientSocket == INVALID_SOCKET)
{
cout<<"TCP Client Socket Creation Failed"<<endl;
cout<<"Error Code - "<<WSAGetLastError()<<endl;
}
cout<<"TCP Client Socket Creation Success"<<endl;

// STEP-3 Fill Server Structure
TCPServerAdd.sin_family = AF_INET;
TCPServerAdd.sin_addr.s_addr =inet_addr("127.0.0.1"); //htonl(INADDR_ANY);
TCPServerAdd.sin_port = htons(8080);

// STEP-4 Connect Fun
iConnect = connect(
                   TCPClientSocket,
                  (SOCKADDR*)&TCPServerAdd,
                  sizeof(TCPServerAdd));
if (iConnect == SOCKET_ERROR)
{
   cout<<"Connection Failed "<<endl;
   cout<<"Error No-> "<<WSAGetLastError()<<endl;
}
cout<<"Connection Success"<<endl;

// STEP-5 RECV Data From Server Side
iRecv = recv(TCPClientSocket,RecvBuffer,iRecvBuffer,0);
if (iRecv == SOCKET_ERROR)
{
 cout<<"Receive Data Failed "<<endl;
 cout<<"Error No-> "<<WSAGetLastError()<<endl;
}
cout<<"DATA RECEIVED -> "<<RecvBuffer<<endl;
memset(&RecvBuffer[0], 0, sizeof(RecvBuffer));
// STEP-6 Send Data to Server
iSend = send(TCPClientSocket,SenderBuffer,iSenderBuffer,0);
if (iSend == SOCKET_ERROR)
{
cout<<"Sending Failed "<<endl;
cout<<"Error No-> "<<WSAGetLastError()<<endl;
}
cout<<"Data Sending Success "<<endl;

// STEP-7 Close Socket Fun
iCloseSocket = closesocket(TCPClientSocket);
if (iCloseSocket == SOCKET_ERROR)
{
cout<<"Closing Socket Failed "<<endl;
cout<<"Error No-> "<<WSAGetLastError()<<endl;
}
cout<<"Closing Socket Success"<<endl;

// STEP-8 WSA CleanUp Fun;
iWsaCleanup = WSACleanup();
if (iWsaCleanup == SOCKET_ERROR)
{
cout<<"CleanUp Fun Failed "<<endl;
cout<<"Error No-> "<<WSAGetLastError()<<endl;
}
cout<<"CleanUp Fun Success"<<endl;

system("PAUSE");
return 0;
}

Buradaki eksiğim ne?
Bunun dışında C/C++ ile soket programlama ve ağ ile ilgili önerebileceğiniz kurs var mı?
 
Son düzenleme:
Üst

Turkhackteam.org internet sitesi 5651 sayılı kanun’un 2. maddesinin 1. fıkrasının m) bendi ile aynı kanunun 5. maddesi kapsamında "Yer Sağlayıcı" konumundadır. İçerikler ön onay olmaksızın tamamen kullanıcılar tarafından oluşturulmaktadır. Turkhackteam.org; Yer sağlayıcı olarak, kullanıcılar tarafından oluşturulan içeriği ya da hukuka aykırı paylaşımı kontrol etmekle ya da araştırmakla yükümlü değildir. Türkhackteam saldırı timleri Türk sitelerine hiçbir zararlı faaliyette bulunmaz. Türkhackteam üyelerinin yaptığı bireysel hack faaliyetlerinden Türkhackteam sorumlu değildir. Sitelerinize Türkhackteam ismi kullanılarak hack faaliyetinde bulunulursa, site-sunucu erişim loglarından bu faaliyeti gerçekleştiren ip adresini tespit edip diğer kanıtlarla birlikte savcılığa suç duyurusunda bulununuz.