A program in C/C++/JAVA/Python for socket programming and share your file from one system to another
Practical 13
A program in
C/C++/JAVA/Python for socket programming and share your file from one system to
another
TCP SERVER AND CLIENT:
InterSystems IRIS supports two Internet Protocols (IP): TCP
and UDP. These Internet Protocol allow InterSystems IRIS processes to
communicate with processes on local or remote systems, whether or not those
processes are running InterSystems IRIS.
- TCP:
the InterSystems IRIS Transmission Control Protocol (TCP) binding.
Establishes a two-way connection between a server and a single client.
Provides reliable byte stream transmission of data with error checking and
correction, and message acknowledgement.
- UDP:
the InterSystems IRIS User Datagram Protocol (UDP) binding. Provides
two-way message transfer between a server and a large number of clients.
UDP is not connection-based; each transmission of data packets is an
independent event. Provides fast and lightweight data transmission for
local packet broadcasts and remote multicasting. Inherently less reliable
than TCP. Does not provide message acknowledgement. For details, refer to
the UDP
Client/Server Communication chapter of this manual.
The “Client” in a TCP/IP
connection is the computer or device that “dials the phone” and the “Server” is
the computer that is “listening” for calls to come in. In other words, the
Client needs to know the IP Address of whatever Server it wants to connect to
and it also needs to know the port number that it wants to send and receive
data through after a connection has been established. The Server only has to
listen for connections and either accept them or reject them when they are
initiated by a client.
Client-Side OPEN Command
OPEN "|TCP|4":("hal":4200::$CHAR(3,4)):10
: x:y means a
specified range of characters from x through y in the ASCII sequence.
| x|y means
specify either x or y.
[ ] Specify zero
or one members of the specified set.
[ ]* Specify zero,
one, or more members of the specified set.
{ } Specify
exactly one member of the specified se
AIM:
A
program in C/C++/JAVA/Python for socket programming and share your file from
one system to another.
:: Server Side ::
#include<WinSock2.h>
#include<iostream>
using namespace std;
int main()
{
cout<<"\t\t------ TCP SERVER
---------"<<endl;
cout<<endl;
WSADATA
Winsockdata ;
int iWsaStartup , iWsaCleanup,
iBind,iListen, iCloseSocket,iRecv,iSend;
SOCKET TCPServerSocket;
struct
sockaddr_in TCPServerAdd;
struct sockaddr_in
TCPClientAdd;
int iTCPClientAdd = sizeof(TCPClientAdd);
SOCKET sAcceptSocket;
char SenderBuffer[512] = "Hello from
Server!";
int iSenderBuffer = strlen( SenderBuffer )+1;
char RecvBuffer[512];
int iRecvBuffer = strlen( RecvBuffer )+1;
// 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");
TCPServerAdd.sin_port = htons(8000);
//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;
// 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;
}
:: Client Side ::
#include<WinSock2.h>
#include<iostream>
using namespace std;
int main()
{
cout<<"\t\t------ TCP CLIENT
--------"<<endl;
cout<<endl;
//Local Variable
WSADATA
WinSockData;
int
iWsaStartup, iWsaCleanup, iSend, iCloseSocket, iConnect, iRecv;
SOCKET
TCPClientSocket;
struct
sockaddr_in TCPServerAdd;
char
RecvBuffer[512];
int
iRecvBuffer = strlen( RecvBuffer )+1;
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");
TCPServerAdd.sin_port = htons(8000);
// 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;
// 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;
}