TUGAS UAS KOMUNIKASI DATA MEMBUAT APLIKASI CHATING MENGGUNAKAN C++
//program client
#include <cstdlib>
#include <iostream>
#include <winsock.h>
#include <windows.h>
#include
<string>
#define CON_PORT 2015
#define REC_PORT 2014
using namespace std;
bool bConnectTo (char* pszAaddress, int nNoPort);
bool bListenOnPort (int nNonPort);
void CloseConnection(SOCKET sock);
DWORD WINAPI Connect (LPVOID Param);
DWORD WINAPI Receive (LPVOID PARAM);
SOCKET sConn;
SOCKET sRecv;
WSADATA wsa1; bConnectTo()
WSADATA wsa2; bListenOnPort()
int main(int argc, char *argv[])
{
bool bStatus;
DWORD id;
bStatus =
bListenOnPort (REC_PORT);
if (bStatus
== false )
{
cout
<< endl << "Listening pada port " << REC_PORT
<< " gagal!!" << endl;
}
else
{
cout
<< endl << "Listening pada port " << REC_PORT
<< "Berhasil!!" << endl;
}
CreateThread
(NULL,0, &Connect,NULL,0, &id);
CreateThread
(NULL,0 ,&Receive, NULL,0,&id);
while (true)
{
char
szInput [1024] = { '\0'};
SOCKET
sSend; // Handle return fungsi send ()
cout
<< "send 1> ";
cin.getline (szInput, 1024);
sSend =
send (sConn,szInput,1024,0); dari szInput
if
(sSend == INVALID_SOCKET)
{
cout
<< "(Gagal Mengirim)" << endl;
}
else
{
cout
<< "Sedding OK)" << endl;
}
}
system
("PAUSE");
return
EXIT_SUCCESS;
}
DWORD WINAPI
Receive (LPVOID Param)
{
SOCKET sAcc;
sAcc = accept
(sRecv,NULL,NULL);
if (sAcc ==
INVALID_SOCKET)
{
cout
<< endl << "Koneksi gagal diterima!!" << endl;
}
else
{
cout
<< endl << "Koneksi berhasil diterima" << endl
<< "send>";
}
while (true)
{
Sleep(100);
char
szMessage[1024] = { '\0' };
SOCKET
sRcv;
sRcv =
recv(sAcc, szMessage, 1024, 0);
if(sRcv
== INVALID_SOCKET)
{
cout
<< endl <<"Gagal Menerima Data!!" << endl;
break;
}
else
{
cout << endl << "pesan atau data dari pengirim :
" << szMessage << endl << "send>";
}
}
}
DWORD WINAPI Connect (LPVOID Param)
{
bool
bKondisi;
while
(true)
{
Sleep
(1000);
bKondisi = bConnectTo("192.168.14.7",CON_PORT);
if
(bKondisi == true)
{
cout << endl << "Koneksi Berhasil" << endl
<< "send>";
break;
}
}
return 0;
}
bool bConnectTo (char* pszAddress, int nNoPort)
{
int error =
WSAStartup (0x0202, &wsa1);
if (error)
{
return false;
}
if
(wsa1.wVersion !=0x0202)
{
WSACleanup ();
return false;
}
SOCKADDR_IN
addr;
addr.sin_family = AF_INET;
addr.sin_port = htons (nNoPort);
addr.sin_addr.s_addr = inet_addr (pszAddress);
sConn =
socket (AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (sConn ==
INVALID_SOCKET)
{
return false;
}
if (connect
(sConn, (SOCKADDR *) & addr, sizeof(addr)) == SOCKET_ERROR)
{
return false;
}
else
{
return true;
}
}
bool bListenOnPort (int nNoPort)
{
int error =
WSAStartup (0x0202, &wsa2);
if (error)
{
return
false;
}
if(wsa2.wVersion !=0x0202)
{
WSACleanup();
return
false;
}
SOCKADDR_IN
addr;
addr.sin_family = AF_INET;
addr.sin_port
= htons(nNoPort);
addr.sin_addr.s_addr = htonl(INADDR_ANY);
sRecv =
socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if(sRecv ==
INVALID_SOCKET)
{
return
false;
}
if(bind(sRecv, (LPSOCKADDR) &addr, sizeof(addr)) == SOCKET_ERROR)
{
return
false;
}
listen
(sRecv,SOMAXCONN);
}
void CloseConnection(SOCKET sock)
{
if (sock)
{
closesocket(sock);
}
WSACleanup();
}
|
//program client
#include <cstdlib>
#include <iostream>
#include <winsock.h>
#include <windows.h>
#include
<string>
#define CON_PORT 2014
#define REC_PORT 2015
using namespace std;
bool bConnectTo (char* pszAaddress, int nNoPort);
bool bListenOnPort (int nNonPort);
void CloseConnection(SOCKET sock);
DWORD WINAPI Connect (LPVOID Param);
DWORD WINAPI Receive (LPVOID PARAM);
SOCKET sConn;
SOCKET sRecv;
WSADATA wsa1; bConnectTo()
WSADATA wsa2; bListenOnPort()
int main(int argc, char *argv[])
{
bool bStatus;
DWORD id;
bStatus =
bListenOnPort (REC_PORT);
if (bStatus
== false )
{
cout
<< endl << "Listening pada port " << REC_PORT
<< " gagal!!" << endl;
}
else
{
cout
<< endl << "Listening pada port " << REC_PORT
<< "Berhasil!!" << endl;
}
CreateThread
(NULL,0, &Connect,NULL,0, &id);
CreateThread
(NULL,0 ,&Receive, NULL,0,&id);
while (true)
{
char
szInput [1024] = { '\0'};
SOCKET
sSend;
cout
<< "send 2> ";
cin.getline (szInput, 1024);
sSend =
send (sConn,szInput,1024,0);
if
(sSend == INVALID_SOCKET)
{
cout
<< "(Gagal Mengirim)" << endl;
}
else
{
cout
<< "Sedding OK)" << endl;
}
}
system
("PAUSE");
return
EXIT_SUCCESS;
}
DWORD WINAPI
Receive (LPVOID Param)
{
SOCKET sAcc;
sAcc = accept
(sRecv,NULL,NULL);
if (sAcc ==
INVALID_SOCKET)
{
cout
<< endl << "Koneksi gagal diterima!!" << endl;
}
else
{
cout
<< endl << "Koneksi berhasil diterima" << endl
<< "send>";
}
while (true)
{
Sleep(100);
char
szMessage[1024] = { '\0' };
SOCKET
sRcv;
sRcv =
recv(sAcc, szMessage, 1024, 0);
if(sRcv
== INVALID_SOCKET)
{
cout
<< endl <<"Gagal Menerima Data!!" << endl;
break;
}
else
{
cout << endl << "pesan atau data pengirim : "
<< szMessage << endl << "send>";
}
}
}
DWORD WINAPI Connect (LPVOID Param)
{
bool
bKondisi;
while
(true)
{
Sleep
(1000);
bKondisi = bConnectTo("192.168.14.2",CON_PORT);
if
(bKondisi == true)
{
cout << endl << "Koneksi Berhasil" << endl
<< "send>";
break;
}
}
return 0;
}
bool bConnectTo (char* pszAddress, int nNoPort)
{
int error =
WSAStartup (0x0202, &wsa1); Winsock
if (error)
{
return false;
}
if
(wsa1.wVersion !=0x0202)
{
WSACleanup ();
return false;
}
SOCKADDR_IN
addr;
addr.sin_family = AF_INET;
addr.sin_port = htons (nNoPort);
addr.sin_addr.s_addr = inet_addr (pszAddress);
sConn =
socket (AF_INET, SOCK_STREAM, IPPROTO_TCP);
socket
if (sConn ==
INVALID_SOCKET)
{
return false;
}
if (connect
(sConn, (SOCKADDR *) & addr, sizeof(addr)) == SOCKET_ERROR)
{
return false;
}
else
{
return true;
}
}
bool bListenOnPort (int nNoPort)
{
int error =
WSAStartup (0x0202, &wsa2); Winsock
if (error)
{
return
false;
}
if(wsa2.wVersion
!=0x0202)
{
WSACleanup();
return
false;
}
SOCKADDR_IN
addr;
addr.sin_family = AF_INET;
addr.sin_port
= htons(nNoPort);
addr.sin_addr.s_addr = htonl(INADDR_ANY);
sRecv =
socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); Socket
if(sRecv ==
INVALID_SOCKET)
{
return
false;
}
if(bind(sRecv, (LPSOCKADDR) &addr, sizeof(addr)) == SOCKET_ERROR)
{
return
false;
}
listen
(sRecv,SOMAXCONN);
}
void CloseConnection(SOCKET sock)
{
if (sock)
{
closesocket(sock);
}
WSACleanup();
}
|
Caranya :