📄 socketprotocol_md.c
字号:
/* * @(#)socketProtocol_md.c 1.21 02/08/30 @(#) * * 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 <errno.h>#include <fcntl.h>#include <stdlib.h>#include <string.h># ifndef BSD_COMP# define BSD_COMP# endif#include <sys/ioctl.h>#include <sys/types.h>#include <sys/socket.h>#include <time.h>#include <netinet/in.h>#include <netinet/tcp.h>#include <netdb.h>#include <arpa/inet.h>#include <socketProtocol.h>#ifndef MAXHOSTNAMELEN#define MAXHOSTNAMELEN 64#endif#define closesocket close#define INVALID_SOCKET (-1)#define SOCKET_ERROR (-1)#define SD_RECEIVE 0#define SD_SEND 1#define SD_BOTH 2#define HENT_BUF_SIZE 1024int 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){ int flgs = fcntl(fd, F_GETFL, 0); fcntl(fd, F_SETFL, flgs | O_NONBLOCK);}/*========================================================================= * 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; struct hostent res; char buf[HENT_BUF_SIZE]; int h_error=0;#ifdef __GLIBC__ gethostbyname_r(host, &res, buf, sizeof(buf), &hp, &h_error);#else hp = gethostbyname_r(host, &res, buf, sizeof(buf), &h_error);#endif /* __GLIBC__ */ 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' res=%lx\n", host, (long)addr.s_addr); }#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 truebuf = KNI_TRUE; 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*)&truebuf, sizeof(int)); addr.sin_family = AF_INET; addr.sin_port = htons((unsigned short)port); addr.sin_addr.s_addr = ipn; res = connect(fd, (struct sockaddr *)&addr, sizeof(addr));#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 != SOCKET_ERROR) { 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; res = recv(fd, p, len, 0); if ((res == -1) && (errno == EWOULDBLOCK)) { res = IO_WOULDBLOCK; } if (res == -1 && errno == EINTR) { res = IO_INTERRUPTED; } return res;}/*========================================================================= * FUNCTION: available 0 * 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){ int n = 0; ioctl(fd, FIONREAD, (void*)&n); return n;}/*========================================================================= * 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; res = send(fd, p, len, 0); if ((res == -1) && (errno == EWOULDBLOCK)) { res = 0; } if (res == -1 && errno == EINTR) { res = IO_INTERRUPTED; } 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){ return closesocket(fd);}/*========================================================================= * 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 truebuf = KNI_TRUE; 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; } setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (char*)&truebuf, 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) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -