参照tcptest.cpp
TCP客户端:
int nClient = socket(AF_INET, SOCK_STREAM, 0);
sockaddr_in serverAddr;
memset(&serverAddr, 0, sizeof(sockaddr_in));
serverAddr.sin_family = AF_INET;
serverAddr.sin_addr.s_addr = inet_addr("127.0.0.1");
serverAddr.sin_port = htons(10000);
int nRet = connect(nClient, (sockaddr*)&serverAddr, sizeof(serverAddr));
int status;
int nPackSize = 45678;
RTPSessionParams sessparams;
sessparams.SetProbationType(RTPSources::NoProbation);
sessparams.SetOwnTimestampUnit(90000.0 / 25.0);
sessparams.SetMaximumPacketSize(nPackSize+64);
m_pTrans = new RTPTCPTransmitter(NULL);//主要用成员变量,用于jrtplib库赋值
status = m_pTrans->Init(true);
status = m_pTrans->Create(65535, NULL);
status = m_RTPSession.Create(sessparams, m_pTrans);
checkerror(status);
status = m_RTPSession.AddDestination(RTPTCPAddress(nClient));
checkerror(status);
TCP服务器:
int nListener = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (nListener == INVALID_SOCKET)
{
return -1;
}
sockaddr_in serverAddr;
memset(&serverAddr, 0, sizeof(sockaddr_in));
serverAddr.sin_family = AF_INET;
serverAddr.sin_addr.S_un.S_addr = INADDR_ANY;
serverAddr.sin_port = htons(10000);
int nRet = bind(nListener, (sockaddr*)&serverAddr, sizeof(serverAddr));
if (nRet == SOCKET_ERROR)
{
return -1;
}
if (listen(nListener, 1) == SOCKET_ERROR)
{
return -1;
}
sockaddr_in clientAddr;
int nLen = sizeof(sockaddr_in);
int nServer = -1;
while (true)
{
nServer = accept(nListener, (sockaddr*)&clientAddr, &nLen);
if (nServer == INVALID_SOCKET)
{
continue;
}
else
{
break;
}
}
int nPackSize = 45678;
RTPSessionParams sessparams;
sessparams.SetProbationType(RTPSources::NoProbation);
sessparams.SetOwnTimestampUnit(90000.0 / 25.0);
sessparams.SetMaximumPacketSize(nPackSize + 64);
pTransparams = new RTPTCPTransmitter(NULL);
//RTPTCPTransmitter transparams(NULL);
status = pTransparams->Init(true);
status = pTransparams->Create(65535, NULL);
status = m_RTPTCPSession.Create(sessparams, pTransparams);
if (status >= 0)
{
m_RTPTCPSession.AddDestination(RTPTCPAddress(nServer));
}