本文整理汇总了C++中PR_NewTCPSocket函数的典型用法代码示例。如果您正苦于以下问题:C++ PR_NewTCPSocket函数的具体用法?C++ PR_NewTCPSocket怎么用?C++ PR_NewTCPSocket使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了PR_NewTCPSocket函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: ar_open
PRFileDesc* Resolver :: ar_open()
{
if (NULL == afd && _res.nscount > 0)
{
if (_res.options & RES_USEVC)
{
struct sockaddr_in *sip;
int i = 0;
sip = _res.nsaddr_list;
ar_vc = 1;
afd = PR_NewTCPSocket();
/*
* Try each name server listed in sequence until we
* succeed or run out.
*/
while (PR_Connect(afd, (PRNetAddr *)sip++,
PR_INTERVAL_NO_TIMEOUT))
{
PR_Close(afd);
afd = NULL;
if (i++ >= _res.nscount)
break;
afd = PR_NewTCPSocket();
}
}
else
{
afd = PR_NewUDPSocket();
}
}
return afd;
}
示例2: DoCallback
// returns 0 on success, non-zero on error
int
DoCallback()
{
UniquePRFileDesc socket(PR_NewTCPSocket());
if (!socket) {
PrintPRError("PR_NewTCPSocket failed");
return 1;
}
PRNetAddr addr;
PR_InitializeNetAddr(PR_IpAddrLoopback, gCallbackPort, &addr);
if (PR_Connect(socket.get(), &addr, PR_INTERVAL_NO_TIMEOUT) != PR_SUCCESS) {
PrintPRError("PR_Connect failed");
return 1;
}
const char *request = "GET / HTTP/1.0\r\n\r\n";
SendAll(socket.get(), request, strlen(request));
char buf[4096];
memset(buf, 0, sizeof(buf));
int32_t bytesRead = PR_Recv(socket.get(), buf, sizeof(buf) - 1, 0,
PR_INTERVAL_NO_TIMEOUT);
if (bytesRead < 0) {
PrintPRError("PR_Recv failed 1");
return 1;
}
if (bytesRead == 0) {
fprintf(stderr, "PR_Recv eof 1\n");
return 1;
}
fprintf(stderr, "%s\n", buf);
return 0;
}
示例3: clientThreadFunc
static void PR_CALLBACK
clientThreadFunc(void *arg)
{
PRUintn port = (PRUintn) arg;
PRFileDesc *sock;
PRNetAddr addr;
char buf[128];
int i;
PRStatus sts;
PRInt32 n;
addr.inet.family = PR_AF_INET;
addr.inet.port = PR_htons((PRUint16)port);
addr.inet.ip = PR_htonl(PR_INADDR_LOOPBACK);
memset(buf, 0, sizeof(buf));
PR_snprintf(buf, sizeof(buf), "%hu", port);
for (i = 0; i < NUM_ITERATIONS; i++) {
sock = PR_NewTCPSocket();
PR_ASSERT(sock != NULL);
sts = PR_Connect(sock, &addr, PR_INTERVAL_NO_TIMEOUT);
PR_ASSERT(sts == PR_SUCCESS);
n = PR_Write(sock, buf, sizeof(buf));
PR_ASSERT(n >= 0);
sts = PR_Close(sock);
PR_ASSERT(sts == PR_SUCCESS);
}
}
示例4: main
int main(int argc, char **argv)
{
PRFileDesc *listenSock;
PRThread *clientThread;
PRThread *serverThread;
PRNetAddr addr;
PRThreadScope scope = PR_GLOBAL_THREAD;
listenSock = PR_NewTCPSocket();
if (NULL == listenSock) {
fprintf(stderr, "PR_NewTCPSocket failed\n");
exit(1);
}
if (PR_InitializeNetAddr(PR_IpAddrAny, 0, &addr) == PR_FAILURE) {
fprintf(stderr, "PR_InitializeNetAddr failed\n");
exit(1);
}
if (PR_Bind(listenSock, &addr) == PR_FAILURE) {
fprintf(stderr, "PR_Bind failed\n");
exit(1);
}
/* Find out what port number we are bound to. */
if (PR_GetSockName(listenSock, &addr) == PR_FAILURE) {
fprintf(stderr, "PR_GetSockName failed\n");
exit(1);
}
if (PR_Listen(listenSock, 5) == PR_FAILURE) {
fprintf(stderr, "PR_Listen failed\n");
exit(1);
}
clientThread = PR_CreateThread(PR_USER_THREAD,
ClientThread, (void *) PR_ntohs(PR_NetAddrInetPort(&addr)),
PR_PRIORITY_NORMAL, scope, PR_JOINABLE_THREAD, 0);
if (NULL == clientThread) {
fprintf(stderr, "PR_CreateThread failed\n");
exit(1);
}
serverThread = PR_CreateThread(PR_USER_THREAD,
ServerThread, listenSock,
PR_PRIORITY_NORMAL, scope, PR_JOINABLE_THREAD, 0);
if (NULL == serverThread) {
fprintf(stderr, "PR_CreateThread failed\n");
exit(1);
}
if (PR_JoinThread(clientThread) == PR_FAILURE) {
fprintf(stderr, "PR_JoinThread failed\n");
exit(1);
}
if (PR_JoinThread(serverThread) == PR_FAILURE) {
fprintf(stderr, "PR_JoinThread failed\n");
exit(1);
}
if (PR_Close(listenSock) == PR_FAILURE) {
fprintf(stderr, "PR_Close failed\n");
exit(1);
}
printf("PASS\n");
return 0;
}
示例5: ConnectThread
static void ConnectThread( void *arg )
{
PRStatus rv;
PRFileDesc *clientSock;
PRNetAddr serverAddress;
clientSock = PR_NewTCPSocket();
PR_ASSERT(clientSock);
if ( resume ) {
if ( debug ) printf("pausing 3 seconds before connect\n");
PR_Sleep( PR_SecondsToInterval(3));
}
memset(&serverAddress, 0, sizeof(serverAddress));
rv = PR_InitializeNetAddr(PR_IpAddrLoopback, BASE_PORT, &serverAddress);
PR_ASSERT( PR_SUCCESS == rv );
rv = PR_Connect( clientSock,
&serverAddress,
PR_SecondsToInterval(1));
PR_ASSERT( PR_SUCCESS == rv );
/* that's all we do. ... Wait for the acceptread() to timeout */
while( state != AllDone )
PR_Sleep( PR_SecondsToInterval(1));
return;
} /* end ConnectThread() */
示例6: ConnectingThread
static void ConnectingThread(void *arg)
{
PRInt32 nbytes;
#ifdef SYMBIAN
char buf[256];
#else
char buf[1024];
#endif
PRFileDesc *sock;
PRNetAddr peer_addr, *addr;
addr = (PRNetAddr*)arg;
sock = PR_NewTCPSocket();
if (sock == NULL)
{
PL_FPrintError(err_out, "PR_NewTCPSocket (client) failed");
PR_ProcessExit(1);
}
if (PR_Connect(sock, addr, PR_INTERVAL_NO_TIMEOUT) == PR_FAILURE)
{
PL_FPrintError(err_out, "PR_Connect (client) failed");
PR_ProcessExit(1);
}
if (PR_GetPeerName(sock, &peer_addr) == PR_FAILURE)
{
PL_FPrintError(err_out, "PR_GetPeerName (client) failed");
PR_ProcessExit(1);
}
/*
** Then wait between the connection coming up and sending the expected
** data. At some point in time, the server should fail due to a timeou
** on the AcceptRead() operation, which according to the document is
** only due to the read() portion.
*/
PR_Sleep(write_dally);
nbytes = PR_Send(sock, GET, sizeof(GET), 0, PR_INTERVAL_NO_TIMEOUT);
if (nbytes == -1) PL_FPrintError(err_out, "PR_Send (client) failed");
nbytes = PR_Recv(sock, buf, sizeof(buf), 0, PR_INTERVAL_NO_TIMEOUT);
if (nbytes == -1) PL_FPrintError(err_out, "PR_Recv (client) failed");
else
{
PR_fprintf(std_out, "PR_Recv (client) succeeded: %d bytes\n", nbytes);
buf[sizeof(buf) - 1] = '\0';
PR_fprintf(std_out, "%s\n", buf);
}
if (PR_FAILURE == PR_Shutdown(sock, PR_SHUTDOWN_BOTH))
PL_FPrintError(err_out, "PR_Shutdown (client) failed");
if (PR_FAILURE == PR_Close(sock))
PL_FPrintError(err_out, "PR_Close (client) failed");
return;
} /* ConnectingThread */
示例7: CreateServerSocket
int CreateServerSocket(struct ThreadData *td) {
/* Create server socket s */
td->fd = PR_NewTCPSocket();
if (td->fd == NULL) return Error(20);
td->r = SSL_ImportFD(NULL, td->fd);
if (td->r == NULL) return Error(21);
return 0;
}
示例8: nssSocket
SslSocket::SslSocket(const std::string& certName, bool clientAuth) :
nssSocket(0), certname(certName), prototype(0), hostnameVerification(true)
{
//configure prototype socket:
prototype = SSL_ImportFD(0, PR_NewTCPSocket());
if (clientAuth) {
NSS_CHECK(SSL_OptionSet(prototype, SSL_REQUEST_CERTIFICATE, PR_TRUE));
NSS_CHECK(SSL_OptionSet(prototype, SSL_REQUIRE_CERTIFICATE, PR_TRUE));
}
}
示例9: TCP_constructor
JSBool
TCP_constructor (JSContext* cx, JSObject* object, uintN argc, jsval* argv, jsval* rval)
{
TCPInformation* data = new TCPInformation;
JS_SetPrivate(cx, object, data);
data->socket = PR_NewTCPSocket();
jsval jsConnected = JSVAL_FALSE;
JS_SetProperty(cx, object, "connected", &jsConnected);
return JS_TRUE;
}
示例10: ClientThread
static void ClientThread(void *arg)
{
PRFileDesc *sock;
PRNetAddr addr;
PRUint16 port = (PRUint16) arg;
char buf[1024];
char *bufPtr;
PRInt32 nbytes;
PRInt32 ntotal;
PRInt32 nexpected;
sock = PR_NewTCPSocket();
if (NULL == sock) {
fprintf(stderr, "PR_NewTCPSocket failed\n");
exit(1);
}
if (PR_InitializeNetAddr(PR_IpAddrLoopback, port, &addr) == PR_FAILURE) {
fprintf(stderr, "PR_InitializeNetAddr failed\n");
exit(1);
}
if (PR_Connect(sock, &addr, PR_INTERVAL_NO_TIMEOUT) == PR_FAILURE) {
fprintf(stderr, "PR_Connect failed\n");
exit(1);
}
ntotal = 0;
bufPtr = buf;
while ((nbytes = PR_Read(sock, bufPtr, sizeof(buf)-ntotal)) > 0) {
ntotal += nbytes;
bufPtr += nbytes;
}
if (-1 == nbytes) {
fprintf(stderr, "PR_Read failed\n");
exit(1);
}
nexpected = HEADER_LEN+TRAILER_LEN+TRAILER_LEN+HEADER_LEN+HEADER_LEN;
if (ntotal != nexpected) {
fprintf(stderr, "total bytes read should be %d but is %d\n",
nexpected, ntotal);
exit(1);
}
if (memcmp(buf, HEADER_STR TRAILER_STR TRAILER_STR HEADER_STR HEADER_STR,
nexpected) != 0) {
fprintf(stderr, "wrong data is received\n");
exit(1);
}
if (PR_Close(sock) == PR_FAILURE) {
fprintf(stderr, "PR_Close failed\n");
exit(1);
}
}
示例11: main
int
main (int argc, char *argv[])
{
if (argc != 3) {
return 1;
}
PRFileDesc* socket = PR_NewTCPSocket();
PRNetAddr addr; initAddr(&addr, argv[1], atoi(argv[2]));
PR_Connect(socket, &addr, PR_INTERVAL_NO_TIMEOUT);
char* string = (char*) malloc(10*sizeof(char));
PR_Recv(socket, string, 10, 0, PR_INTERVAL_NO_TIMEOUT);
string[10] = 0;
printf("%s\n", string);
PR_Close(socket);
}
示例12: DEFINE_CONSTRUCTOR
/**doc
$TOC_MEMBER $INAME
$INAME( [type = Socket.TCP] )
Type can be Socket.TCP or Socket.UDP.
**/
DEFINE_CONSTRUCTOR() {
PRFileDesc *fd = NULL;
JL_DEFINE_ARGS;
JL_ASSERT_CONSTRUCTING();
{
JL_DEFINE_CONSTRUCTOR_OBJ;
int descType;
if ( JL_ARG_ISDEF(1) )
JL_CHK( jl::getValue(cx, JL_ARG(1), &descType) );
else
descType = PR_DESC_SOCKET_TCP; // default
if ( descType == PR_DESC_SOCKET_TCP )
fd = PR_NewTCPSocket();
else if ( descType == PR_DESC_SOCKET_UDP )
fd = PR_NewUDPSocket();
else
JL_ERR( E_ARG, E_NUM(1), E_INVALID );
if ( fd == NULL )
return ThrowIoError(cx);
JL_CHK( jl::reserveStreamReadInterface(cx, JL_OBJ) );
JL_SetPrivate(JL_OBJ, fd);
}
return true;
bad:
if ( fd )
PR_Close(fd);
return false;
}
示例13: SSL_ImportFD
int SslSocket::listen(uint16_t port, int backlog, const std::string& certName, bool clientAuth) const
{
//configure prototype socket:
prototype = SSL_ImportFD(0, PR_NewTCPSocket());
if (clientAuth) {
NSS_CHECK(SSL_OptionSet(prototype, SSL_REQUEST_CERTIFICATE, PR_TRUE));
NSS_CHECK(SSL_OptionSet(prototype, SSL_REQUIRE_CERTIFICATE, PR_TRUE));
}
//get certificate and key (is this the correct way?)
CERTCertificate *cert = PK11_FindCertFromNickname(const_cast<char*>(certName.c_str()), 0);
if (!cert) throw Exception(QPID_MSG("Failed to load certificate '" << certName << "'"));
SECKEYPrivateKey *key = PK11_FindKeyByAnyCert(cert, 0);
if (!key) throw Exception(QPID_MSG("Failed to retrieve private key from certificate"));
NSS_CHECK(SSL_ConfigSecureServer(prototype, cert, key, NSS_FindCertKEAType(cert)));
SECKEY_DestroyPrivateKey(key);
CERT_DestroyCertificate(cert);
//bind and listen
const int& socket = impl->fd;
int yes=1;
QPID_POSIX_CHECK(setsockopt(socket,SOL_SOCKET,SO_REUSEADDR,&yes,sizeof(yes)));
struct sockaddr_in name;
name.sin_family = AF_INET;
name.sin_port = htons(port);
name.sin_addr.s_addr = 0;
if (::bind(socket, (struct sockaddr*)&name, sizeof(name)) < 0)
throw Exception(QPID_MSG("Can't bind to port " << port << ": " << strError(errno)));
if (::listen(socket, backlog) < 0)
throw Exception(QPID_MSG("Can't listen on port " << port << ": " << strError(errno)));
socklen_t namelen = sizeof(name);
if (::getsockname(socket, (struct sockaddr*)&name, &namelen) < 0)
throw QPID_POSIX_ERROR(errno);
return ntohs(name.sin_port);
}
示例14: do_GetService
// nr_socket APIs (as member functions)
int NrSocket::create(nr_transport_addr *addr) {
int r,_status;
PRStatus status;
PRNetAddr naddr;
nsresult rv;
nsCOMPtr<nsISocketTransportService> stservice =
do_GetService(NS_SOCKETTRANSPORTSERVICE_CONTRACTID, &rv);
if (!NS_SUCCEEDED(rv)) {
ABORT(R_INTERNAL);
}
if((r=nr_transport_addr_to_praddr(addr, &naddr)))
ABORT(r);
switch (addr->protocol) {
case IPPROTO_UDP:
if (!(fd_ = PR_NewUDPSocket())) {
r_log(LOG_GENERIC,LOG_CRIT,"Couldn't create socket");
ABORT(R_INTERNAL);
}
break;
case IPPROTO_TCP:
if (!(fd_ = PR_NewTCPSocket())) {
r_log(LOG_GENERIC,LOG_CRIT,"Couldn't create socket");
ABORT(R_INTERNAL);
}
break;
default:
ABORT(R_INTERNAL);
}
status = PR_Bind(fd_, &naddr);
if (status != PR_SUCCESS) {
r_log(LOG_GENERIC,LOG_CRIT,"Couldn't bind socket to address %s",
addr->as_string);
ABORT(R_INTERNAL);
}
r_log(LOG_GENERIC,LOG_DEBUG,"Creating socket %p with addr %s",
fd_, addr->as_string);
nr_transport_addr_copy(&my_addr_,addr);
/* If we have a wildcard port, patch up the addr */
if(nr_transport_addr_is_wildcard(addr)){
status = PR_GetSockName(fd_, &naddr);
if (status != PR_SUCCESS){
r_log(LOG_GENERIC, LOG_CRIT, "Couldn't get sock name for socket");
ABORT(R_INTERNAL);
}
if((r=nr_praddr_to_transport_addr(&naddr,&my_addr_,addr->protocol,1)))
ABORT(r);
}
// Set nonblocking
PRSocketOptionData option;
option.option = PR_SockOpt_Nonblocking;
option.value.non_blocking = PR_TRUE;
status = PR_SetSocketOption(fd_, &option);
if (status != PR_SUCCESS) {
r_log(LOG_GENERIC, LOG_CRIT, "Couldn't make socket nonblocking");
ABORT(R_INTERNAL);
}
// Remember our thread.
ststhread_ = do_QueryInterface(stservice, &rv);
if (!NS_SUCCEEDED(rv))
ABORT(R_INTERNAL);
// Finally, register with the STS
rv = stservice->AttachSocket(fd_, this);
if (!NS_SUCCEEDED(rv)) {
ABORT(R_INTERNAL);
}
_status = 0;
abort:
return(_status);
}
示例15: PR_IMPLEMENT
PR_IMPLEMENT(PRStatus) PR_NewTCPSocketPair(PRFileDesc *f[])
{
#ifdef XP_UNIX
PRInt32 rv, osfd[2];
if (!_pr_initialized) _PR_ImplicitInitialization();
rv = _PR_MD_SOCKETPAIR(AF_UNIX, SOCK_STREAM, 0, osfd);
if (rv == -1) {
return PR_FAILURE;
}
f[0] = PR_AllocFileDesc(osfd[0], PR_GetTCPMethods());
if (!f[0]) {
_PR_MD_CLOSE_SOCKET(osfd[0]);
_PR_MD_CLOSE_SOCKET(osfd[1]);
/* PR_AllocFileDesc() has invoked PR_SetError(). */
return PR_FAILURE;
}
f[1] = PR_AllocFileDesc(osfd[1], PR_GetTCPMethods());
if (!f[1]) {
PR_Close(f[0]);
_PR_MD_CLOSE_SOCKET(osfd[1]);
/* PR_AllocFileDesc() has invoked PR_SetError(). */
return PR_FAILURE;
}
_PR_MD_MAKE_NONBLOCK(f[0]);
_PR_MD_INIT_FD_INHERITABLE(f[0], PR_FALSE);
_PR_MD_MAKE_NONBLOCK(f[1]);
_PR_MD_INIT_FD_INHERITABLE(f[1], PR_FALSE);
return PR_SUCCESS;
#elif defined(WINNT)
/*
* A socket pair is often used for interprocess communication,
* so we need to make sure neither socket is associated with
* the I/O completion port; otherwise it can't be used by a
* child process.
*
* The default implementation below cannot be used for NT
* because PR_Accept would have associated the I/O completion
* port with the listening and accepted sockets.
*/
SOCKET listenSock;
SOCKET osfd[2];
struct sockaddr_in selfAddr, peerAddr;
int addrLen;
if (!_pr_initialized) _PR_ImplicitInitialization();
osfd[0] = osfd[1] = INVALID_SOCKET;
listenSock = socket(AF_INET, SOCK_STREAM, 0);
if (listenSock == INVALID_SOCKET) {
goto failed;
}
selfAddr.sin_family = AF_INET;
selfAddr.sin_port = 0;
selfAddr.sin_addr.s_addr = htonl(INADDR_LOOPBACK); /* BugZilla: 35408 */
addrLen = sizeof(selfAddr);
if (bind(listenSock, (struct sockaddr *) &selfAddr,
addrLen) == SOCKET_ERROR) {
goto failed;
}
if (getsockname(listenSock, (struct sockaddr *) &selfAddr,
&addrLen) == SOCKET_ERROR) {
goto failed;
}
if (listen(listenSock, 5) == SOCKET_ERROR) {
goto failed;
}
osfd[0] = socket(AF_INET, SOCK_STREAM, 0);
if (osfd[0] == INVALID_SOCKET) {
goto failed;
}
selfAddr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
/*
* Only a thread is used to do the connect and accept.
* I am relying on the fact that connect returns
* successfully as soon as the connect request is put
* into the listen queue (but before accept is called).
* This is the behavior of the BSD socket code. If
* connect does not return until accept is called, we
* will need to create another thread to call connect.
*/
if (connect(osfd[0], (struct sockaddr *) &selfAddr,
addrLen) == SOCKET_ERROR) {
goto failed;
}
/*
* A malicious local process may connect to the listening
* socket, so we need to verify that the accepted connection
* is made from our own socket osfd[0].
*/
if (getsockname(osfd[0], (struct sockaddr *) &selfAddr,
&addrLen) == SOCKET_ERROR) {
goto failed;
}
osfd[1] = accept(listenSock, (struct sockaddr *) &peerAddr, &addrLen);
if (osfd[1] == INVALID_SOCKET) {
goto failed;
//.........这里部分代码省略.........