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

📄 socketprotocol_md.c

📁 用于移动设备上的java虚拟机源代码
💻 C
📖 第 1 页 / 共 2 页
字号:
/* * @(#)socketProtocol_md.c	1.27 02/10/09 @(#) * * Copyright (c) 1999-2002 Sun Microsystems, Inc.  All rights reserved. * PROPRIETARY/CONFIDENTIAL * Use is subject to license terms. *//*========================================================================= * KVM *========================================================================= * SYSTEM:    KVM * SUBSYSTEM: networking (Generic Connection framework) * FILE:      socketProtocol.c * OVERVIEW:  This file provides a default implementation of the native *            functions that are needed for supporting the "socket:" and *            "serversocket:" Generic Connection protocols. * AUTHOR(s): Nik Shaylor, Efren Serra *//** * This implements the network protocols for Windows * * @author  Nik Shaylor * @version 1.0 1/17/2000 *//*========================================================================= * Include files *=======================================================================*/#include <kni.h>#include <stdio.h>#include <stdlib.h>#include <string.h>#include <time.h>#define WIN32_LEAN_AND_MEAN#define NOMSG#include <windows.h>#include <process.h>#ifdef GCC#include <winsock.h>#else#include <winsock2.h>#endif#include <socketProtocol.h>#define MAXHOSTNAMELEN 256WSADATA wsaData;int netError(void);/*========================================================================= * Protocol methods *=======================================================================*//*========================================================================= * FUNCTION:      setNonBlocking() * CLASS:         com.sun.midp.io.j2me.socket.Protocol * TYPE:          virtual native function * OVERVIEW:      Translate a host name into an ip address *=======================================================================*/void prim_com_sun_midp_io_j2me_socket_Protocol_setNonBlocking(int fd){    unsigned long blockingFlag = 1;#if INCLUDEDEBUGCODE        if (tracenetworking) {            fprintf(stdout, "setNonBlocking fd=%d\n", fd);        }#endif /* INCLUDEDEBUGCODE */    ioctlsocket(fd, FIONBIO, &blockingFlag);}/*========================================================================= * FUNCTION:      getIpNumber() * CLASS:         com.sun.midp.io.j2me.socket.Protocol * TYPE:          virtual native function * OVERVIEW:      Translate a host name into an ip address *=======================================================================*/int prim_com_sun_midp_io_j2me_socket_Protocol_getIpNumber(char *host){    struct hostent *hp;    struct in_addr addr;    hp = gethostbyname(host);    if (hp == NULL ) {#if INCLUDEDEBUGCODE        if (tracenetworking) {            fprintf(stdout, "getIpNumber host='%s' res=-1\n", host);        }#endif /* INCLUDEDEBUGCODE */        return -1;    }    memcpy(&addr, hp->h_addr_list[0], hp->h_length);#if INCLUDEDEBUGCODE    if (tracenetworking) {        fprintf(stdout, "getIpNumber host='%s' s_addr=%lx %u.%u.%u.%u\n", host,                (long)addr.s_addr, hp->h_addr_list[0][0] & 0xff,                 hp->h_addr_list[0][1] & 0xff, hp->h_addr_list[0][2] & 0xff,                 hp->h_addr_list[0][3] & 0xff);    }#endif /* INCLUDEDEBUGCODE */    return addr.s_addr;}/*========================================================================= * FUNCTION:      open0() * CLASS:         com.sun.midp.io.j2me.socket.Protocol * TYPE:          virtual native function * OVERVIEW:      Open a TCP socket *=======================================================================*/int prim_com_sun_midp_io_j2me_socket_Protocol_open0(char *name, int port,                                                    char **exception){    int falsebuf  = FALSE;    struct sockaddr_in addr;    int fd = -1, ipn, res;    ipn = prim_com_sun_midp_io_j2me_socket_Protocol_getIpNumber(name);    if (ipn == -1) {        *exception = "javax/microedition/io/ConnectionNotFoundException";        return -1;    }    fd = socket(AF_INET, SOCK_STREAM, 0);    if (fd == INVALID_SOCKET) {        *exception = "java/io/IOException";        return -1;    }    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 = ipn;    do {        res = connect(fd, (struct sockaddr *)&addr, sizeof(addr));    } while ((res < 0) && (errno == WSAEINTR || errno == WSAEALREADY));#if INCLUDEDEBUGCODE        if (tracenetworking) {            fprintf(stdout,                    "socket::open0-connect ipn=%lx port=%ld res=%ld ne=%ld\n",                    (long)ipn, (long)port, (long)res, (long)netError());        }#endif /* INCLUDEDEBUGCODE */    if (res < 0 && errno == WSAEISCONN) {        return fd;    }    if (res >= 0) {        return fd;    }    closesocket(fd);    *exception = "javax/microedition/io/ConnectionNotFoundException";    return -1;}/*========================================================================= * FUNCTION:      read0() * CLASS:         com.sun.midp.io.j2me.socket.Protocol * TYPE:          virtual native function * OVERVIEW:      Read from a TCP socket *=======================================================================*/int prim_com_sun_midp_io_j2me_socket_Protocol_read0(int fd, char *p, int len){    int res;    int lastError;    res = recv(fd, p, len, 0);    if (res == SOCKET_ERROR) {        lastError = netError();        if (lastError == WSAEWOULDBLOCK) {            res = IO_WOULDBLOCK;        } else if (lastError == WSAEINTR) {            res = IO_INTERRUPTED;        } else {            res = -1;        }    }    return res;}/*========================================================================= * FUNCTION:      available0 * CLASS:         com.sun.midp.io.j2me.socket.Protocol * TYPE:          virtual native function * OVERVIEW:      Return the number of bytes available *=======================================================================*/int prim_com_sun_midp_io_j2me_socket_Protocol_available0(int fd){    unsigned long len;    int res = ioctlsocket(fd, FIONREAD, &len);    if (res < 0) {        return 0;    } else {        return len;    }}/*========================================================================= * FUNCTION:      write0() * CLASS:         com.sun.midp.io.j2me.socket.Protocol * TYPE:          virtual native function * OVERVIEW:      Write to a TCP socket *=======================================================================*/int prim_com_sun_midp_io_j2me_socket_Protocol_write0(int fd, char *p, int len){    int res;    int lastError;    res = send(fd, p, len, 0);    if (res == SOCKET_ERROR) {        lastError = netError();        if (lastError == WSAEWOULDBLOCK) {            res = 0;        } else if (lastError == WSAEINTR) {            res = IO_INTERRUPTED;        } else {            res = -1;        }    }    return res;}/*========================================================================= * FUNCTION:      shutdownOutput() * CLASS:         com.sun.midp.io.j2me.socket.Protocol * TYPE:          virtual native function * OVERVIEW:      Shutdown the output side of a TCP socket *=======================================================================*/void prim_com_sun_midp_io_j2me_socket_Protocol_shutdownOutput0(int fd){    shutdown(fd, SD_SEND);}/*========================================================================= * FUNCTION:      close0() * CLASS:         com.sun.midp.io.j2me.socket.Protocol * TYPE:          virtual native function * OVERVIEW:      Close a TCP socket *=======================================================================*/int prim_com_sun_midp_io_j2me_socket_Protocol_close0(int fd){    int res;    int lastError;    res = closesocket(fd);    if (res == SOCKET_ERROR) {        lastError = netError();        if (lastError == WSAEWOULDBLOCK) {            /* call closesocket again, see doc for closesocket */            closesocket(fd);            res = 0;        } else {            res = -1;        }    }    return res;}/*========================================================================= * FUNCTION:      open() * CLASS:         com.sun.midp.io.j2me.serversocket.Protocol * TYPE:          virtual native function * OVERVIEW:      Open a listening TCP socket *=======================================================================*/int prim_com_sun_midp_io_j2me_serversocket_Protocol_open0(int port,                                                          char **exception){    int falsebuf  = FALSE;    struct sockaddr_in addr;    int fd = -1, res;    fd = socket(AF_INET, SOCK_STREAM, 0);    if (fd == INVALID_SOCKET) {        *exception = "java/io/IOException";        return -1;    }

⌨️ 快捷键说明

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