⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 socketprotocol_md.c

📁 用于移动设备上的java虚拟机源代码
💻 C
📖 第 1 页 / 共 2 页
字号:
    setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (char*)&falsebuf, sizeof (int));     addr.sin_family      = AF_INET;    addr.sin_port        = htons((unsigned short)port);    addr.sin_addr.s_addr = htonl(INADDR_ANY);    res = bind(fd, (struct sockaddr*)&addr, sizeof(addr));    if (res == SOCKET_ERROR) {        *exception = "java/io/IOException";    } else {        res = listen(fd, 3);        if (res == SOCKET_ERROR) {            *exception = "java/io/IOException";        } else {            /* success */            return fd;        }    }    closesocket(fd);    return -1;}/*========================================================================= * FUNCTION:      accept() * CLASS:         com.sun.midp.io.j2me.serversocket.Protocol * TYPE:          virtual native function * OVERVIEW:      Accept and open a connection *=======================================================================*/int prim_com_sun_midp_io_j2me_serversocket_Protocol_accept(int fd){    int res;    struct sockaddr sa;    int saLen = sizeof (sa);    res = accept(fd, &sa, &saLen);    if (res == SOCKET_ERROR) {        if (netError() == WSAEWOULDBLOCK) {            res = IO_WOULDBLOCK;        } else {            res = -1;        }    }    return res;}/*========================================================================= * FUNCTION:      close0() * CLASS:         com.sun.midp.io.j2me.serversocket.Protocol * TYPE:          virtual native function * OVERVIEW:      Close a TCP socket *=======================================================================*/int prim_com_sun_midp_io_j2me_serversocket_Protocol_close(int fd){    return prim_com_sun_midp_io_j2me_socket_Protocol_close0(fd);}/*========================================================================= * FUNCTION:      wsaInit() * TYPE:          machine-specific implementation of native function * OVERVIEW:      Initialize the windows socket system * INTERFACE: *   parameters:  none *   returns:     none *=======================================================================*/static int netinit = 0;void networkInit(void) {    if (!netinit) {	if (WSAStartup(0x0101, &wsaData ) != 0) {	    KNI_FatalError("Windows sockets WSAtartup failure");	}	netinit = 1;    }}/*========================================================================= * FUNCTION:      netError() * TYPE:          machine-specific implementation of native function * OVERVIEW:      return fatal windows socket error * INTERFACE: *   parameters:  none *   returns:     int *=======================================================================*/int netError(void) {    return WSAGetLastError();}/*========================================================================= * FUNCTION:      getsockopt0() * CLASS:         com.sun.midp.io.j2me.socket.Protocol * TYPE:          virtual native function * OVERVIEW:      Get a socket option value. *=======================================================================*/int prim_com_sun_midp_io_j2me_socket_Protocol_getsockopt0(int fd, int flag, int *optval){    int level = SOL_SOCKET;    int optname;    int optsize = sizeof(optname);    struct linger lbuf ;    void * opttarget = (void *) optval ;    switch (flag) {     case 0: /* DELAY */	level = IPPROTO_TCP;	optname = TCP_NODELAY;	break;    case 1: /* LINGER */	opttarget = (void *) &lbuf ;	optsize = sizeof (struct linger);	optname = SO_LINGER;	break;    case 2: /* KEEPALIVE */	optname = SO_KEEPALIVE;	break;    case 3: /* RCVBUF */	optname = SO_RCVBUF;	break;    case 4: /* SNDBUF */	optname = SO_SNDBUF;	break;    default: /* IllegalArgumentException */	KNI_ThrowNew("java/lang/IllegalArgumentException","");	return -1;    }    if (getsockopt(fd, level,  optname, opttarget, &optsize) == 0 ) {	if (optname == SO_LINGER) {	    /* If linger is on return the number of seconds. */	    *optval = (lbuf.l_onoff == 0 ? 0 : lbuf.l_linger) ;	}#if INCLUDEDEBUGCODE        if (tracenetworking) {  	    fprintf(stdout, "getSockopt option=%d value=%d size=%d\n", 		optname, *optval, optsize);        }#endif /* INCLUDEDEBUGCODE */        return 0;    } else {	fprintf (stderr, "getsockopt errno=%d option=%d level=%d\n", 		 errno, optname, level );	return -1;    }}/*========================================================================= * FUNCTION:      setsockopt0() * CLASS:         com.sun.midp.io.j2me.socket.Protocol * TYPE:          virtual native function * OVERVIEW:      Set a socket option value. *=======================================================================*/int prim_com_sun_midp_io_j2me_socket_Protocol_setsockopt0(int fd, int flag, int optval){    int    level = SOL_SOCKET;    int    optsize =  sizeof(optval);    int    optname;    struct linger lbuf ;    void * opttarget = (void *) & optval ;    switch (flag) {     case 0: /* DELAY */	level = IPPROTO_TCP;	optname = TCP_NODELAY;	break;    case 1: /* LINGER */	opttarget = (void *) &lbuf ;	optsize = sizeof (struct linger);	optname = SO_LINGER;	if (optval == 0) {	    lbuf.l_onoff = 0;	    lbuf.l_linger = 0;	} else {	    lbuf.l_onoff = 1;	    lbuf.l_linger = optval;	}	    	break;    case 2: /* KEEPALIVE */	optname = SO_KEEPALIVE;	break;    case 3: /* RCVBUF */	optname = SO_RCVBUF;	break;    case 4: /* SNDBUF */	optname = SO_SNDBUF;	break;    default: /* IllegalArgumentException */	KNI_ThrowNew("java/lang/IllegalArgumentException", "");	return -1;    }    if (setsockopt(fd, level,  optname, opttarget, optsize) == 0){#if INCLUDEDEBUGCODE    if (tracenetworking) {	fprintf(stdout, "setSockopt option=%d value=%d\n", 		optname, optval);        }#endif /* INCLUDEDEBUGCODE */    } else {	fprintf (stderr, "setsockopt errno=%d\n", errno);	return -1;    }        return 0;}/*========================================================================= * FUNCTION:      getipnumber1() * CLASS:         com.sun.midp.io.j2me.socket.Protocol * TYPE:          virtual native function * OVERVIEW:      Get the IP address of the local or remote socket endpoint. *=======================================================================*/char * prim_com_sun_midp_io_j2me_socket_Protocol_getipnumber1(int fd, int local){    int res;    struct sockaddr_in sa;    int saLen = sizeof (sa);    if (local) {	res = getsockname(fd, (struct sockaddr*)&sa, &saLen);    } else {	res = getpeername(fd, (struct sockaddr*)&sa, &saLen);    }    if (res < 0) {        return NULL;    }#if INCLUDEDEBUGCODE    if (tracenetworking) {	fprintf(stdout, "get ip number family=%x port=%d addr=%s\n",  		sa.sin_family, sa.sin_port, inet_ntoa(sa.sin_addr) );        }#endif /* INCLUDEDEBUGCODE */    return inet_ntoa(sa.sin_addr);}	/*========================================================================= * FUNCTION:      getport1() * CLASS:         com.sun.midp.io.j2me.socket.Protocol * TYPE:          virtual native function * OVERVIEW:      Get the port number of the local or remote socket endpoint. *=======================================================================*/int prim_com_sun_midp_io_j2me_socket_Protocol_getport1(int fd, int local){    int res;    struct sockaddr_in sa;    int saLen = sizeof (sa);    if (local) {	res = getsockname(fd, (struct sockaddr*)&sa, &saLen);    } else {	res = getpeername(fd, (struct sockaddr*)&sa, &saLen);    }#if INCLUDEDEBUGCODE    if (tracenetworking) {	fprintf(stdout, "get ip number family=%x port=%d addr=%s\n",  		sa.sin_family, sa.sin_port, inet_ntoa(sa.sin_addr) );        }#endif /* INCLUDEDEBUGCODE */    return ntohs(sa.sin_port);}        /*========================================================================= * FUNCTION:      getLocalIPNumber_md() * TYPE:          common native function * OVERVIEW:      Get the IP address of the local device. *=======================================================================*/int getLocalIPNumber_md() {    char hostname[MAXHOSTNAMELEN];    struct hostent *hp;    gethostname(hostname, sizeof (hostname));#if INCLUDEDEBUGCODE    if (tracenetworking) {        fprintf(stdout, "local hostname = %s\n", hostname);    }#endif /* INCLUDEDEBUGCODE */    hp = gethostbyname(hostname);    return ((struct in_addr*)*hp->h_addr_list)->s_addr;}/*========================================================================= * FUNCTION:      getLocalIPAddressAsString_md() * TYPE:          common native function * OVERVIEW:      Get the IP address of the local device. *=======================================================================*/char *getLocalIPAddressAsString_md() {    struct in_addr addr;    addr.s_addr = getLocalIPNumber_md();    return inet_ntoa(addr);}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -