当前位置:  编程技术>java/j2ee

java调用c程序通信示例代码

    来源: 互联网  发布时间:2014-10-30

    本文导语:  代码如下://===============Client Struct================#pragma pack(push,4) #define LOG_SEND 0#define MSG_SEND 1#define EXIT_SEND 2#define BUFFER_MAX_SIZE 512#define HEADER_LEN  sizeof(Header)typedef struct HeaderStruct{ int OP;//OP : 0--> Login  1--> SendMsg  2--> Exit int size;}Header...

代码如下:

//===============Client Struct================
#pragma pack(push,4)

#define LOG_SEND 0
#define MSG_SEND 1
#define EXIT_SEND 2
#define BUFFER_MAX_SIZE 512
#define HEADER_LEN  sizeof(Header)
typedef struct HeaderStruct
{
 int OP;//OP : 0--> Login  1--> SendMsg  2--> Exit
 int size;
}Header;

//#define LOG_INFO_TIME_OFFSET   (sizeof(Log) - 2 * sizeof(char *))
#define LOG_INFO_TIME_OFFSET  (sizeof(Log) - 2 * sizeof(int))//Modify 2009年7月15日15:15:14
#define LOG_INFO_USERNAME_OFFSET(pLog)   (LOG_INFO_TIME_OFFSET + pLog->timeLen* 2 )
typedef struct LogStruct
{
 int timeLen;
 int userNameLen;
 char* time;
 char* userName; 
}Log;
//#define SENDMSG_INFO_USERNAME_OFFSET   (sizeof(SendMsg) - 3 * sizeof(char *))
#define SENDMSG_INFO_USERNAME_OFFSET  (sizeof(SendMsg) - 3 * sizeof(int))
#define SENDMSG_INFO_SENDMSG_OFFSET(pSendMsg)   (SENDMSG_INFO_USERNAME_OFFSET + pSendMsg->userNameLen * 2)
#define SENDMSG_INFO_TIME_OFFSET(pSendMsg)   (SENDMSG_INFO_SENDMSG_OFFSET(pSendMsg) + pSendMsg->sendMsgLen * 2)
typedef struct SendMsgStruct
{
 int userNameLen;
 int sendMsgLen;
 int timeLen;
 char* userName;
 char* sendMsg;
 char* time;
}SendMsg;
#pragma pack(pop)

代码如下:

#include
#include
#include
#include
#include
#include "winsock2.h"
#include "SendStruct.h"
unsigned __stdcall SendThread(void* socket);

//Unicode转换为Ascii
void uni2str(const LPWSTR wstr, char *str)
{
 int  len;

 len = wcslen(wstr);
 if(len == 0 ){
  str[0] = 0;
  return;
 }
 memset(str,0,(len+1)*2);
 WideCharToMultiByte(CP_ACP,0,(LPWSTR)wstr,len,str,(len*2 + 1),NULL, NULL);
}
//Ascii转换为Unicode
void str2uni(const char *str, LPWSTR wstr)
{
 int  len;
 len = strlen(str);
 if(len == 0 ){
  wstr[0] = 0;
  return ;
 }
 memset((char*)wstr,0,(len+1)*2);
 MultiByteToWideChar(CP_ACP,0,str,len, wstr,(len+1) * 2);
}
//Unicode主机序转换为网络序
void unih2n(LPWSTR uniStr)
{
    for(; *uniStr != 0; uniStr++){
        *uniStr = htons((short)*uniStr);
    }
}
//Unicode网络序转换为主机序
void unin2h(LPWSTR uniStr)
{
 int          i;
 int    len ;
 len = wcslen((wchar_t*)uniStr);
    for(i=0;iOP = htonl(retCmdId);
 pCmdHeader->size = htonl(pCmdHeader->size);
 tmpBuf = buff + HEADER_LEN;
 if(retCmdId == LOG_SEND)//Send Log Info
 {
  pLog = (Log *)tmpBuf;
  //========================================
  pLog->timeLen = htonl(pLog->timeLen);
  pLog->userNameLen = htonl(pLog->userNameLen);
  //========================================
  uniStr = (LPWSTR)(tmpBuf + LOG_INFO_TIME_OFFSET);
  str2uni(pLog->time,uniStr);
  unih2n(uniStr);
  dataLen += pLog->timeLen ;
  uniStr = (LPWSTR)(tmpBuf + LOG_INFO_USERNAME_OFFSET(pLog));
  str2uni(pLog->userName,uniStr);
  unih2n(uniStr);
  dataLen += pLog->userNameLen;
 }
 else if(retCmdId == MSG_SEND)//Send Msg Info
 {
  pSendMsg = (SendMsg *)tmpBuf;
  //========================================
  pSendMsg->userNameLen = htonl(pSendMsg->userNameLen);
  pSendMsg->sendMsgLen = htonl(pSendMsg->sendMsgLen);
  pSendMsg->timeLen = htonl(pSendMsg->timeLen);
  //========================================
  uniStr = (LPWSTR)(tmpBuf + SENDMSG_INFO_USERNAME_OFFSET);
  str2uni(pSendMsg->userName,uniStr);
  unih2n(uniStr);
  dataLen += pSendMsg->userNameLen * 2;
  uniStr = (LPWSTR)(tmpBuf + SENDMSG_INFO_SENDMSG_OFFSET(pSendMsg));
  str2uni(pSendMsg->sendMsg,uniStr);
  unih2n(uniStr);
  dataLen += pSendMsg->sendMsgLen * 2;
  uniStr = (LPWSTR)(tmpBuf + SENDMSG_INFO_TIME_OFFSET(pSendMsg));
  str2uni(pSendMsg->time,uniStr);
  unih2n(uniStr);
  dataLen += pSendMsg->timeLen * 2;
 }
 pCmdHeader->size = htonl(dataLen);
 dataLen += HEADER_LEN;
 return dataLen;
}

//构建数据函数 retCmdId根据Header中的OP来的。把现有数据构建到buffer(包含头信息和数据信息)中
int constructDataBuffBySource(int retCmdId,char *buff,char *buffSource)
{
 Header*      pCmdHeader;
 Log*            pLog;
 SendMsg*   pSendMsg;
 int                dataLen = 0;
 char*      tmpBuf;
 LPWSTR    uniStr;
 char             tmp[512];
 LPWSTR     tmpUniStr;
 wchar_t       uniChar;
 pCmdHeader = (Header *)buff;
 pCmdHeader->OP = htonl(retCmdId);
 tmpBuf = buff + HEADER_LEN;
 if(retCmdId == LOG_SEND)//Send Log Info
 {
  pLog = (Log *)tmpBuf;
  //将buffSource转换为Log结构
  Log * pTmpLog = (Log *)(buffSource);
  //========================================
  pLog->timeLen = htonl(pTmpLog->timeLen);
  pLog->userNameLen = htonl(pTmpLog->userNameLen);
  //========================================
  dataLen = LOG_INFO_TIME_OFFSET;
  //找到buffSource对应time的OFFSET。
  tmpUniStr = (LPWSTR)(buffSource + LOG_INFO_TIME_OFFSET);
  uniChar = tmpUniStr[pTmpLog->timeLen];
  tmpUniStr[pTmpLog->timeLen] = 0;
  //将tmpUniStr里的值转换成本地
  unin2h(tmpUniStr);
  //将Unicode转换为Ascii。并赋值给tmp数组
  uni2str(tmpUniStr,tmp);
  tmpUniStr[pTmpLog->timeLen] = uniChar;
  uniStr = (LPWSTR)(tmpBuf + LOG_INFO_TIME_OFFSET);
  str2uni(tmp,uniStr);
  unih2n(uniStr);
  dataLen += pLog->timeLen  * 2;
  tmpUniStr = (LPWSTR)(buffSource + LOG_INFO_USERNAME_OFFSET(pTmpLog) );
  tmpUniStr[pTmpLog->userNameLen] = 0;
  //将tmpUniStr里的值转换成本地
  unin2h(tmpUniStr);
  //将Unicode转换为Ascii。并赋值给tmp数组
  uni2str(tmpUniStr,tmp);
  uniStr = (LPWSTR)(tmpBuf + LOG_INFO_USERNAME_OFFSET(pLog));
  str2uni(tmp,uniStr);
  unih2n(uniStr);
  dataLen += pLog->userNameLen * 2;
 }
 else if(retCmdId == MSG_SEND)//Send Msg Info
 {
  pSendMsg = (SendMsg *)tmpBuf;
  SendMsg * pTmpSendMsg = (SendMsg *)(buffSource);
  //========================================
  pSendMsg->userNameLen = htonl(pTmpSendMsg->userNameLen);
  pSendMsg->sendMsgLen = htonl(pTmpSendMsg->sendMsgLen);
  pSendMsg->timeLen = htonl(pTmpSendMsg->timeLen);
  //========================================

  dataLen = SENDMSG_INFO_USERNAME_OFFSET;
  //找到buffSource对应userName的OFFSET。
  tmpUniStr = (LPWSTR)(buffSource + SENDMSG_INFO_USERNAME_OFFSET);
  uniChar = tmpUniStr[pTmpSendMsg->userNameLen];
  tmpUniStr[pTmpSendMsg->userNameLen] = 0;
  //将tmpUniStr里的值转换成本地
  unin2h(tmpUniStr);
  //将Unicode转换为Ascii。并赋值给tmp数组
  uni2str(tmpUniStr,tmp);
  tmpUniStr[pTmpSendMsg->userNameLen] = uniChar;
  uniStr = (LPWSTR)(tmpBuf + SENDMSG_INFO_USERNAME_OFFSET);
  str2uni(tmp,uniStr);
  unih2n(uniStr);
  dataLen += pSendMsg->userNameLen * 2;
  //找到buffSource对应sendMsg的OFFSET。
  tmpUniStr = (LPWSTR)(buffSource + SENDMSG_INFO_SENDMSG_OFFSET(pTmpSendMsg));
  uniChar = tmpUniStr[pTmpSendMsg->userNameLen];
  tmpUniStr[pTmpSendMsg->userNameLen] = 0;
  //将tmpUniStr里的值转换成本地
  unin2h(tmpUniStr);
  //将Unicode转换为Ascii。并赋值给tmp数组
  uni2str(tmpUniStr,tmp);
  tmpUniStr[pTmpSendMsg->userNameLen] = uniChar;
  uniStr = (LPWSTR)(tmpBuf + SENDMSG_INFO_SENDMSG_OFFSET(pSendMsg));
  str2uni(tmp,uniStr);
  unih2n(uniStr);
  dataLen += pSendMsg->sendMsgLen * 2;
  //找到buffSource对应time的OFFSET。
  tmpUniStr = (LPWSTR)(buffSource + SENDMSG_INFO_TIME_OFFSET(pTmpSendMsg));
  tmpUniStr[pTmpSendMsg->userNameLen] = 0;
  //将tmpUniStr里的值转换成本地
  unin2h(tmpUniStr);
  //将Unicode转换为Ascii。并赋值给tmp数组
  uni2str(tmpUniStr,tmp);
  uniStr = (LPWSTR)(tmpBuf + SENDMSG_INFO_TIME_OFFSET(pSendMsg));
  str2uni(tmp,uniStr);
  unih2n(uniStr);
  dataLen += pSendMsg->timeLen * 2;
 }
 pCmdHeader->size = htonl(dataLen);
 dataLen += HEADER_LEN;
 return dataLen;
}

int constructDataBuffByLog(char *buff, Log * logBuffSource)
{
 Header*      pCmdHeader;
 Log*            pLog;
 int                dataLen = 0;
 char*      tmpBuf;
 LPWSTR    uniStr;
 //int tmpLenTime,tmpLenName;
 pCmdHeader = (Header *)buff;
 pCmdHeader->OP = htonl(0);
 tmpBuf = buff + HEADER_LEN;

 pLog = (Log *)tmpBuf;
 pLog->timeLen = logBuffSource->timeLen;
 pLog->userNameLen = logBuffSource->userNameLen;
 dataLen = LOG_INFO_TIME_OFFSET;
 uniStr = (LPWSTR)(tmpBuf + LOG_INFO_TIME_OFFSET);
 str2uni(logBuffSource->time,uniStr);
 unih2n(uniStr);
 dataLen += pLog->timeLen  * 2;
 int len = LOG_INFO_USERNAME_OFFSET(pLog);
 uniStr = (LPWSTR)(tmpBuf + LOG_INFO_USERNAME_OFFSET(pLog));
 str2uni(logBuffSource->userName,uniStr);
 unih2n(uniStr);
 dataLen += pLog->userNameLen * 2;

 pLog->timeLen = htonl(pLog->timeLen);
 pLog->userNameLen = htonl(pLog->userNameLen);

 pCmdHeader->size = htonl(dataLen);
 dataLen += HEADER_LEN;
 return dataLen;
}
int constructDataBuffBySendMsg(char *buff,SendMsg * sendMsgSource)
{
 Header*      pCmdHeader;
 SendMsg*   pSendMsg;
 int                dataLen = 0;
 char*      tmpBuf;
 LPWSTR    uniStr;
 pCmdHeader = (Header *)buff;
 pCmdHeader->OP = htonl(1);
 tmpBuf = buff + HEADER_LEN;
 pSendMsg = (SendMsg *)tmpBuf;
 pSendMsg->userNameLen = sendMsgSource->userNameLen;
 pSendMsg->sendMsgLen = sendMsgSource->sendMsgLen;
 pSendMsg->timeLen = sendMsgSource->timeLen;
 dataLen = SENDMSG_INFO_USERNAME_OFFSET;
 uniStr = (LPWSTR)(tmpBuf + SENDMSG_INFO_USERNAME_OFFSET);
 printf("in constructDataBuffBySendMsg  --- the sendMsgSource->userName is %s/n",sendMsgSource->userName);
 str2uni(sendMsgSource->userName, uniStr);
 unih2n(uniStr);
 dataLen += pSendMsg->userNameLen * 2;
 uniStr = (LPWSTR)(tmpBuf + SENDMSG_INFO_SENDMSG_OFFSET(pSendMsg));
 //=======error=======
 int len = SENDMSG_INFO_SENDMSG_OFFSET(pSendMsg);
 printf("%s",sendMsgSource->sendMsg);
 str2uni(sendMsgSource->sendMsg,uniStr);
 unih2n(uniStr);
 dataLen += pSendMsg->sendMsgLen * 2;
 uniStr = (LPWSTR)(tmpBuf + SENDMSG_INFO_TIME_OFFSET(pSendMsg));
 str2uni(sendMsgSource->time,uniStr);
 unih2n(uniStr);
 dataLen += pSendMsg->timeLen * 2;
 pSendMsg->userNameLen = htonl(pSendMsg->userNameLen);
 pSendMsg->sendMsgLen = htonl(pSendMsg->sendMsgLen);
 pSendMsg->timeLen = htonl(pSendMsg->timeLen);
 pCmdHeader->size = htonl(dataLen);
 dataLen += HEADER_LEN;
 return dataLen;
}
//转换所接收数据,并打印输出,不包含头信息
void convertDataAndPrint(int cmdId,char *buff, unsigned int dataLen)
{
 Log *pLog;
 SendMsg *pSendMsg;
 char tmp[512];
 //char * tmpBuf;
 LPWSTR uniStr;
 wchar_t uniChar;
 //int i;
 unsigned int len;

 printf("/n=====================================================/n");
 if(cmdId == LOG_SEND)
 {
  pLog = (Log *)(buff);
  pLog->timeLen = ntohl(pLog->timeLen);
  pLog->userNameLen = ntohl(pLog->userNameLen);
  len =LOG_INFO_TIME_OFFSET + pLog->timeLen * 2;
  if(len > dataLen)
  {
   printf("错误:数据解析越界,时间长度无效(%d)/n", pLog->timeLen);
   return;
  }
  //time
  uniStr = (LPWSTR)(buff + LOG_INFO_TIME_OFFSET);
  //这里是把uniStr所切取的字符串最后一位字符给uniChar(因为该位上可能是下一个数据的值)。
  //再让uniStr最后一位为0,成为一个字符串。最后将uniChar里的值还回去
  uniChar = uniStr[pLog->timeLen];
  //the end is '/0'
  uniStr[pLog->timeLen] = 0;
  //Unicode network order Trans To Host order
  unin2h(uniStr);
  //Unicode Trans To AscII,tmp is char(single char)
  uni2str(uniStr,tmp);
  uniStr[pLog->timeLen] = uniChar;
  printf("[%s]:  ",tmp);  

  len += pLog->userNameLen * 2;
  if(len > dataLen)
  {
   printf("错误:数据解析越界,姓名长度无效(%d)/n", pLog->userNameLen);
   return;
  }
  //userName
  uniStr = (LPWSTR)(buff + LOG_INFO_USERNAME_OFFSET(pLog));
  //uniChar = uniStr[pLog->userNameLen];
  uniStr[pLog->userNameLen] = 0;
  unin2h(uniStr);
  uni2str(uniStr,tmp);
  //uniStr[pLog->userNameLen] = uniChar;
  printf("%s  connected.../n",tmp);  
  printf("=====================LogInfo End=======================/n");
 }
 else if(cmdId == MSG_SEND)
 {
  pSendMsg = (SendMsg *)buff;
  pSendMsg->userNameLen = ntohl(pSendMsg->userNameLen);
  pSendMsg->sendMsgLen = ntohl(pSendMsg->sendMsgLen);
  pSendMsg->timeLen = ntohl(pSendMsg->timeLen);
  len = SENDMSG_INFO_USERNAME_OFFSET + pSendMsg->userNameLen * 2;
  if(len > dataLen)
  {
   printf("错误:数据解析越界,姓名长度无效(%d)/n", pSendMsg->userNameLen);
   return;
  }
  //userName
  uniStr = (LPWSTR)(buff + SENDMSG_INFO_USERNAME_OFFSET);
  uniChar = uniStr[pSendMsg->userNameLen];
  uniStr[pSendMsg->userNameLen] = 0;
  unin2h(uniStr);
  uni2str(uniStr,tmp);
  uniStr[pSendMsg->userNameLen] = uniChar;
  printf("[%s]   ",tmp);
  len += pSendMsg->sendMsgLen * 2;
  if(len > dataLen)
  {
   printf("错误:数据解析越界,信息长度无效(%d)/n",pSendMsg->sendMsgLen);
   return;
  }

  //sendMsg  
  long len2 =SENDMSG_INFO_SENDMSG_OFFSET(pSendMsg);
  len2 = ntohl(len2);
  uniStr = (LPWSTR)(buff + SENDMSG_INFO_SENDMSG_OFFSET(pSendMsg));
  //long len2 =SENDMSG_INFO_SENDMSG_OFFSET(pSendMsg);
  uniChar = uniStr[pSendMsg->sendMsgLen];
  uniStr[pSendMsg->sendMsgLen] = 0;
  unin2h(uniStr);
  uni2str(uniStr,tmp);
  uniStr[pSendMsg->sendMsgLen] = uniChar;
  printf("  %s  ",tmp);
  len += pSendMsg->timeLen * 2;
  if(len > dataLen)
  {
   printf("错误:数据解析越界,时间长度无效(%d)/n",pSendMsg->timeLen);
   return;
  }
  //time
  uniStr = (LPWSTR) (buff + SENDMSG_INFO_TIME_OFFSET(pSendMsg));
  uniStr[pSendMsg->timeLen] = 0;
  unin2h(uniStr);
  uni2str(uniStr,tmp);
  printf(" [%s] /n",tmp);
  printf("====================SendMsgInfo End====================/n");
 }
}
void main() {
 // 检查 Winsock 版本号,WSAData为WSADATA结构对象
 WSADATA wsaData;
 int iResult = WSAStartup(MAKEWORD(2,2), &wsaData);
 if (iResult != NO_ERROR)
  printf("Error at WSAStartup()/n");
 //创建套接字
 SOCKET ConnectSocket;
 ConnectSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
 if (ConnectSocket == INVALID_SOCKET) {
  printf("Error at socket(): %ld/n", WSAGetLastError());
  WSACleanup();
  return;
 }
 //填写远程地址信息
 sockaddr_in clientService;
 clientService.sin_family = AF_INET;
 clientService.sin_port = htons( 27015 );
 //填写服务器程序所在的机器的IP地址
    clientService.sin_addr.s_addr = inet_addr( "127.0.0.1" );
    //连接服务器端
 if ( connect( ConnectSocket, (SOCKADDR*) &clientService, sizeof(clientService) ) == SOCKET_ERROR)
 {
  printf( "Failed to connect./n" );
  WSACleanup();
  return;
 }
    //创建句柄
 HANDLE hCliThread;
 unsigned threadID;
    //产生线程 3.线程创建后调用的函数,4.该函数的参数,6.线程ID 方便对线程进行管理
 hCliThread = (HANDLE)_beginthreadex(NULL,0,SendThread,(void *)ConnectSocket,0,&threadID);
 int bytesRecv = 0;
    //创建一个接收的结构体,用于接收并解析
 Header *recvHeader = (Header *)malloc(HEADER_LEN);
 char* buffer;
 buffer = (char *)malloc(BUFFER_MAX_SIZE);
 for(;;)//recevice data from server
 {
        //接收连接上来的服务端,在客户器端创建一个socket为ConnectSocket
  bytesRecv = recv(ConnectSocket,(char *)recvHeader,HEADER_LEN,0);
  if(bytesRecv OP = ntohl(recvHeader->OP);
  recvHeader->size = ntohl(recvHeader->size);
  printf("recv HeaderInfo: OP: %d ,size : %d /n",recvHeader->OP,recvHeader->size);
  if(recvHeader->size > 0 && recvHeader->size < BUFFER_MAX_SIZE)
  {
   bytesRecv = recv(ConnectSocket,buffer,recvHeader->size,0);
   if(bytesRecv OP,buffer,recvHeader->size);
  }
 }
 free(buffer);
 WSACleanup();
 return;
}
unsigned __stdcall SendThread(void *socket)//send data to otherclient and server
{
 Header header;
 SendMsg sendMsg;
 Log log;
 int dataLen;
 char *buffer;
 char pUserName[40];
 char pSendMsg[512];
 //int userNameLen;
 char time[8] = "2009";
 buffer = (char *)malloc(BUFFER_MAX_SIZE);
 printf("======Welcome  to YY ChatRoom======/n");
 printf("========Log========:/n");
 //==================登录 并发送登录信息=================
 printf("Your Name : ");
 scanf("%s",pUserName);
 printf("your name is: %s/n",pUserName);
 log.userName = pUserName;
 log.userNameLen = strlen(log.userName);
 //_strtime(time);
 log.time = time;
 log.timeLen = strlen(log.time);

 header.OP = 0;
 dataLen = constructDataBuffByLog(buffer,&log);
 int sendLen = send((SOCKET)socket,buffer,dataLen,0);
 printf("the DataLen is : %d  the SendLen is %d/n",dataLen,sendLen);
 if (sendLen < 0)
 {
  printf("Client: disconnect with server,(%d)", WSAGetLastError());
  return 0;
 }
 memset(buffer,0,BUFFER_MAX_SIZE);
 //==================登录 并发送登录信息 结束=================
 //循环发送数据信息给server端
 while(1)
 {
  sendMsg.userName = pUserName;
  sendMsg.userNameLen =strlen(pUserName);
  printf("Input:");
  scanf("%s",pSendMsg);
  sendMsg.sendMsg = pSendMsg;
  sendMsg.sendMsgLen = strlen(sendMsg.sendMsg);
  //_strtime(time);
  sendMsg.time = time;
  sendMsg.timeLen = strlen(sendMsg.time);
  header.OP = htonl(1);
  //header.size = htonl(sendMsg.userNameLen + sendMsg.sendMsgLen + sendMsg.timeLen);
  dataLen = constructDataBuffBySendMsg(buffer,&sendMsg);
  int sendLen = send((SOCKET)socket,buffer,dataLen,0);
  if(sendLen  < 0)
  {
   printf("Client: disconnect with server/n");
      break;
  }
  printf("the dataLen is (%d),the sendLen is(%d)/n",dataLen,sendLen);
  memset(buffer,0,BUFFER_MAX_SIZE);
 }
    //结束线程
 free(buffer);
 _endthreadex( 0 );
 return 0;
}

代码如下:

#define BACKLOG (int)20 /* 多少等待连接控制*/
#include
#include
#include "winsock2.h"
#include
#include "SendStruct.h"
unsigned __stdcall SockThread(void *socket);
//CRITICALSECTION CriticalSection;//define critical resource
SOCKET socketArr[BACKLOG];//save socket from client
HOSTENT *host;
//struct MsgStruct clientNameStr[BACKLOG];//save recv name from client
static int socketLen = 0;
sockaddr_in remoteAddr;//接受的socket
//Unicode转换为AscII
void uni2str(const LPWSTR wstr,char *str)
{
 int len;
 len = wcslen(wstr);
 if(len == 0)
 {
  str[0] = 0;
  return;
 }
 memset(str,0,(len + 1) * 2);
 WideCharToMultiByte(CP_ACP,0,(LPWSTR)wstr,len,str,(len * 2 + 1),NULL,NULL);
}
//AscII转换为Unicode
void str2uni(const char *str,LPWSTR wstr)
{
 int len;
 len = strlen(str);
 if(len == 0)
 {
  wstr[0] = 0;
  return;
 }
 memset((char *)wstr,0,(len + 1) * 2);
 MultiByteToWideChar(CP_ACP,0,str,len,wstr,(len + 1) * 2);
}
//Unicode主机序转换为网络序
void unih2n(LPWSTR uniStr)
{
 for(; *uniStr != 0;uniStr++)
 {
  *uniStr = htons((short)*uniStr);
 }
}
//Unicode网络序转换为主机序
void unin2h(LPWSTR uniStr)
{
 int i;
 int len;
 len = wcslen((wchar_t*)uniStr);
 for(i = 0;i < len;i++)
 {
  uniStr[i] = ntohs((short)uniStr[i]);
 }
}
//构建数据函数 retCmdId根据Header中的OP来的。把数据构建到buffer(包含头信息和数据信息)中
int constructDataBuff(int retCmdId,char *buff)
{
 //Header*      pCmdHeader = (Header *)malloc(HEADER_LEN);
 Log*            pLog;
 SendMsg*   pSendMsg;
 int                dataLen = 0;
 char*      tmpBuf;
 LPWSTR    uniStr;
 //pCmdHeader = (Header *)buff;
 //pCmdHeader->OP = htonl(retCmdId);
 //pCmdHeader->size = htonl(pCmdHeader->size);
 //tmpBuf = buff + HEADER_LEN;
 tmpBuf = buff;
 if(retCmdId == LOG_SEND)//Send Log Info
 {
  pLog = (Log *)tmpBuf;
  //========================================

  //========================================
  uniStr = (LPWSTR)(tmpBuf + LOG_INFO_TIME_OFFSET);
  str2uni(pLog->time,uniStr);
  unih2n(uniStr);
  dataLen += pLog->timeLen ;
  uniStr = (LPWSTR)(tmpBuf + LOG_INFO_USERNAME_OFFSET(pLog));
  str2uni(pLog->userName,uniStr);
  unih2n(uniStr);
  dataLen += pLog->userNameLen;
  pLog->timeLen = htonl(pLog->timeLen);
  pLog->userNameLen = htonl(pLog->userNameLen);
 }
 else if(retCmdId == MSG_SEND)//Send Msg Info
 {
  pSendMsg = (SendMsg *)tmpBuf;
  //========================================
  //pSendMsg->userNameLen = pSendMsg->userNameLen;
  //pSendMsg->sendMsgLen =pSendMsg->sendMsgLen;
  //pSendMsg->timeLen = pSendMsg->timeLen;
  //========================================
  uniStr = (LPWSTR)(tmpBuf + SENDMSG_INFO_USERNAME_OFFSET);
  str2uni(pSendMsg->userName,uniStr);
  unih2n(uniStr);
  dataLen += pSendMsg->userNameLen * 2;
  uniStr = (LPWSTR)(tmpBuf + SENDMSG_INFO_SENDMSG_OFFSET(pSendMsg));
  int tmpLen = SENDMSG_INFO_SENDMSG_OFFSET(pSendMsg);
  //tmpLen = pSendMsg->sendMsgLen;
  str2uni(pSendMsg->sendMsg,uniStr);
  unih2n(uniStr);
  dataLen += pSendMsg->sendMsgLen * 2;
  uniStr = (LPWSTR)(tmpBuf + SENDMSG_INFO_TIME_OFFSET(pSendMsg));
  tmpLen = SENDMSG_INFO_TIME_OFFSET(pSendMsg);
  tmpLen = pSendMsg->timeLen;
  str2uni(pSendMsg->time,uniStr);
  unih2n(uniStr);
  dataLen += pSendMsg->timeLen * 2;
  pSendMsg->userNameLen = htonl(pSendMsg->userNameLen);
  pSendMsg->sendMsgLen = htonl(pSendMsg->sendMsgLen);
  pSendMsg->timeLen = htonl(pSendMsg->timeLen);
 }
 //pCmdHeader->size = htonl(dataLen);
 dataLen += HEADER_LEN;
 return dataLen;
}

//构建数据函数 retCmdId根据Header中的OP来的。把现有数据构建到buffer(包含头信息和数据信息)中
int constructDataBuffBySource(int retCmdId,char *buff,char *buffSource)
{
 Header*      pCmdHeader;
 Log*            pLog;
 SendMsg*   pSendMsg;
 int                dataLen = 0;
 char*      tmpBuf;
 LPWSTR    uniStr;
 char             tmp[512];
 LPWSTR     tmpUniStr;
 wchar_t       uniChar;
 pCmdHeader = (Header *)buff;
 pCmdHeader->OP = htonl(retCmdId);
 tmpBuf = buff + HEADER_LEN;
 if(retCmdId == LOG_SEND)//Send Log Info
 {
  pLog = (Log *)tmpBuf;
  //将buffSource转换为Log结构
  Log * pTmpLog = (Log *)(buffSource);
  //========================================
  pLog->timeLen = htonl(pTmpLog->timeLen);
  pLog->userNameLen = htonl(pTmpLog->userNameLen);
  //========================================
  dataLen = LOG_INFO_TIME_OFFSET;
  //找到buffSource对应time的OFFSET。
  tmpUniStr = (LPWSTR)(buffSource + LOG_INFO_TIME_OFFSET);
  uniChar = tmpUniStr[pTmpLog->timeLen];
  tmpUniStr[pTmpLog->timeLen] = 0;
  //将tmpUniStr里的值转换成本地
  //*****************unin2h(tmpUniStr);
  //将Unicode转换为Ascii。并赋值给tmp数组
  uni2str(tmpUniStr,tmp);
  tmpUniStr[pTmpLog->timeLen] = uniChar;
  uniStr = (LPWSTR)(tmpBuf + LOG_INFO_TIME_OFFSET);
  str2uni(tmp,uniStr);
  unih2n(uniStr);
  dataLen += pLog->timeLen  * 2;
  tmpUniStr = (LPWSTR)(buffSource + LOG_INFO_USERNAME_OFFSET(pTmpLog) );
  tmpUniStr[pTmpLog->userNameLen] = 0;
  //将tmpUniStr里的值转换成本地
  unin2h(tmpUniStr);
  //将Unicode转换为Ascii。并赋值给tmp数组
  uni2str(tmpUniStr,tmp);
  uniStr = (LPWSTR)(tmpBuf + LOG_INFO_USERNAME_OFFSET(pLog));
  str2uni(tmp,uniStr);
  unih2n(uniStr);
  dataLen += pLog->userNameLen * 2;
 }
 else if(retCmdId == MSG_SEND)//Send Msg Info
 {
  pSendMsg = (SendMsg *)tmpBuf;
  SendMsg * pTmpSendMsg = (SendMsg *)(buffSource);
  //========================================
  pSendMsg->userNameLen = htonl(pTmpSendMsg->userNameLen);
  pSendMsg->sendMsgLen = htonl(pTmpSendMsg->sendMsgLen);
  pSendMsg->timeLen = htonl(pTmpSendMsg->timeLen);
  //========================================

  dataLen = SENDMSG_INFO_USERNAME_OFFSET;
  //找到buffSource对应userName的OFFSET。
  tmpUniStr = (LPWSTR)(buffSource + SENDMSG_INFO_USERNAME_OFFSET);
  uniChar = tmpUniStr[pTmpSendMsg->userNameLen];
  tmpUniStr[pTmpSendMsg->userNameLen] = 0;
  //将tmpUniStr里的值转换成本地
  unin2h(tmpUniStr);
  //将Unicode转换为Ascii。并赋值给tmp数组
  uni2str(tmpUniStr,tmp);
  tmpUniStr[pTmpSendMsg->userNameLen] = uniChar;
  uniStr = (LPWSTR)(tmpBuf + SENDMSG_INFO_USERNAME_OFFSET);
  str2uni(tmp,uniStr);
  unih2n(uniStr);
  dataLen += pSendMsg->userNameLen * 2;
  //找到buffSource对应sendMsg的OFFSET。
  tmpUniStr = (LPWSTR)(buffSource + SENDMSG_INFO_SENDMSG_OFFSET(pTmpSendMsg));
  uniChar = tmpUniStr[pTmpSendMsg->userNameLen];
  tmpUniStr[pTmpSendMsg->userNameLen] = 0;
  //将tmpUniStr里的值转换成本地
  unin2h(tmpUniStr);
  //将Unicode转换为Ascii。并赋值给tmp数组
  uni2str(tmpUniStr,tmp);
  tmpUniStr[pTmpSendMsg->userNameLen] = uniChar;
  uniStr = (LPWSTR)(tmpBuf + SENDMSG_INFO_SENDMSG_OFFSET(pSendMsg));
  str2uni(tmp,uniStr);
  unih2n(uniStr);
  dataLen += pSendMsg->sendMsgLen * 2;
  //找到buffSource对应time的OFFSET。
  tmpUniStr = (LPWSTR)(buffSource + SENDMSG_INFO_TIME_OFFSET(pTmpSendMsg));
  tmpUniStr[pTmpSendMsg->userNameLen] = 0;
  //将tmpUniStr里的值转换成本地
  unin2h(tmpUniStr);
  //将Unicode转换为Ascii。并赋值给tmp数组
  uni2str(tmpUniStr,tmp);
  uniStr = (LPWSTR)(tmpBuf + SENDMSG_INFO_TIME_OFFSET(pSendMsg));
  str2uni(tmp,uniStr);
  unih2n(uniStr);
  dataLen += pSendMsg->timeLen * 2;
 }
 pCmdHeader->size = htonl(dataLen);
 dataLen += HEADER_LEN;
 return dataLen;
}

int constructDataBuffByLog(char *buff, Log * logBuffSource)
{
 Header*      pCmdHeader;
 Log*            pLog;
 int                dataLen = 0;
 char*      tmpBuf;
 LPWSTR    uniStr;
 pCmdHeader = (Header *)buff;
 pCmdHeader->OP = htonl(0);
 tmpBuf = buff + HEADER_LEN;

 pLog = (Log *)tmpBuf;
 //========================================
 pLog->timeLen = htonl(logBuffSource->timeLen);
 pLog->userNameLen = htonl(logBuffSource->userNameLen);
 //========================================
 dataLen = LOG_INFO_TIME_OFFSET;
 uniStr = (LPWSTR)(tmpBuf + LOG_INFO_TIME_OFFSET);
 str2uni(logBuffSource->time,uniStr);
 unih2n(uniStr);
 dataLen += pLog->timeLen  * 2;
 uniStr = (LPWSTR)(tmpBuf + LOG_INFO_USERNAME_OFFSET(pLog));
 str2uni(logBuffSource->userName,uniStr);
 unih2n(uniStr);
 dataLen += pLog->userNameLen * 2;

 pCmdHeader->size = htonl(dataLen);
 dataLen += HEADER_LEN;
 return dataLen;
}

int constructDataBuffBySendMsg(char *buff,SendMsg * sendMsgSource)
{
 Header*      pCmdHeader;
 SendMsg*   pSendMsg;
 int                dataLen = 0;
 char*      tmpBuf;
 LPWSTR    uniStr;
 pCmdHeader = (Header *)buff;
 pCmdHeader->OP = htonl(1);
 tmpBuf = buff + HEADER_LEN;
 pSendMsg = (SendMsg *)tmpBuf;
 pSendMsg->userNameLen = sendMsgSource->userNameLen;
 pSendMsg->sendMsgLen = sendMsgSource->sendMsgLen;
 pSendMsg->timeLen = sendMsgSource->timeLen;
 dataLen = SENDMSG_INFO_USERNAME_OFFSET;
 uniStr = (LPWSTR)(tmpBuf + SENDMSG_INFO_USERNAME_OFFSET);
 str2uni(sendMsgSource->userName,uniStr);
 unih2n(uniStr);
 dataLen += pSendMsg->userNameLen * 2;
 uniStr = (LPWSTR)(tmpBuf + SENDMSG_INFO_SENDMSG_OFFSET(pSendMsg));
 //=======error=======
 int len = SENDMSG_INFO_SENDMSG_OFFSET(pSendMsg);
 printf("%s",sendMsgSource->sendMsg);
 str2uni(sendMsgSource->sendMsg,uniStr);
 unih2n(uniStr);
 dataLen += pSendMsg->sendMsgLen * 2;
 uniStr = (LPWSTR)(tmpBuf + SENDMSG_INFO_TIME_OFFSET(pSendMsg));
 str2uni(sendMsgSource->time,uniStr);
 unih2n(uniStr);
 dataLen += pSendMsg->timeLen * 2;
 pSendMsg->userNameLen = htonl(pSendMsg->userNameLen);
 pSendMsg->sendMsgLen = htonl(pSendMsg->sendMsgLen);
 pSendMsg->timeLen = htonl(pSendMsg->timeLen);
 pCmdHeader->size = htonl(dataLen);
 dataLen += HEADER_LEN;
 return dataLen;
}
//转换所接收数据,并打印输出,不包含头信息
void convertDataAndPrint(int cmdId,char *buff, unsigned int dataLen)
{
 Log *pLog;
 SendMsg *pSendMsg;
 char tmp[512];
 LPWSTR uniStr;
 wchar_t uniChar;
 unsigned int len;

 printf("=====================================================/n");
 if(cmdId == LOG_SEND)
 {
  pLog = (Log *)(buff);
  pLog->timeLen = ntohl(pLog->timeLen);
  pLog->userNameLen = ntohl(pLog->userNameLen);
  len =LOG_INFO_TIME_OFFSET + pLog->timeLen * 2;
  if(len > dataLen)
  {
   printf("错误:数据解析越界,时间长度无效(%d)/n", pLog->timeLen);
   return;
  }
  //time
  uniStr = (LPWSTR)(buff + LOG_INFO_TIME_OFFSET);
  //这里是把uniStr所切取的字符串最后一位字符给uniChar(因为该位上可能是下一个数据的值)。
  //再让uniStr最后一位为0,成为一个字符串。最后将uniChar里的值还回去
  uniChar = uniStr[pLog->timeLen];
  //the end is '/0'
  uniStr[pLog->timeLen] = 0;
  //Unicode network order Trans To Host order
  unin2h(uniStr);
  //Unicode Trans To AscII,tmp is char(single char)
  uni2str(uniStr,tmp);
  uniStr[pLog->timeLen] = uniChar;
  printf("[%s]:  ",tmp);  

  len += pLog->userNameLen * 2;
  if(len > dataLen)
  {
   printf("错误:数据解析越界,姓名长度无效(%d)/n", pLog->userNameLen);
   return;
  }
  //userName
  uniStr = (LPWSTR)(buff + LOG_INFO_USERNAME_OFFSET(pLog));
  uniChar = uniStr[pLog->userNameLen];
  uniStr[pLog->userNameLen] = 0;
  unin2h(uniStr);
  uni2str(uniStr,tmp);
  uniStr[pLog->userNameLen] = uniChar;
  printf("%s  connected.../n",tmp);  
  printf("=====================LogInfo End=======================/n");
 }
 else if(cmdId == MSG_SEND)
 {
  pSendMsg = (SendMsg *)buff;
  pSendMsg->userNameLen = ntohl(pSendMsg->userNameLen);
  pSendMsg->sendMsgLen = ntohl(pSendMsg->sendMsgLen);
  pSendMsg->timeLen = ntohl(pSendMsg->timeLen);
  len = SENDMSG_INFO_USERNAME_OFFSET + pSendMsg->userNameLen * 2;
  if(len > dataLen)
  {
   printf("错误:数据解析越界,姓名长度无效(%d)/n", pSendMsg->userNameLen);
   return;
  }
  //userName
  uniStr = (LPWSTR)(buff + SENDMSG_INFO_USERNAME_OFFSET);
  uniChar = uniStr[pSendMsg->userNameLen];
  uniStr[pSendMsg->userNameLen] = 0;
  unin2h(uniStr);
  uni2str(uniStr,tmp);
  uniStr[pSendMsg->userNameLen] = uniChar;
  printf("[%s]   ",tmp);
  len += pSendMsg->sendMsgLen * 2;
  if(len > dataLen)
  {
   printf("错误:数据解析越界,信息长度无效(%d)/n",pSendMsg->sendMsgLen);
   return;
  }

  //sendMsg
  uniStr = (LPWSTR)(buff + SENDMSG_INFO_SENDMSG_OFFSET(pSendMsg));
  uniChar = uniStr[pSendMsg->sendMsgLen];
  uniStr[pSendMsg->sendMsgLen] = 0;
  unin2h(uniStr);
  uni2str(uniStr,tmp);
  uniStr[pSendMsg->sendMsgLen] = uniChar;
  printf("  %s  ",tmp);
  len += pSendMsg->timeLen * 2;
  if(len > dataLen)
  {
   printf("错误:数据解析越界,时间长度无效(%d)/n",pSendMsg->timeLen);
   return;
  }
  //time
  uniStr = (LPWSTR) (buff + SENDMSG_INFO_TIME_OFFSET(pSendMsg));
  uniChar = uniStr[pSendMsg->sendMsgLen];
  uniStr[pSendMsg->timeLen] = 0;
  unin2h(uniStr);
  uni2str(uniStr,tmp);
  uniStr[pSendMsg->sendMsgLen] = uniChar;
  printf(" [%s]/n",tmp);
  printf("====================SendMsgInfo End====================/n");
 }
}
void main()
{
    //INITIALIZECRITICALSECTION CriticalSection;
 int nAddrLen = sizeof(remoteAddr);
 SOCKET sClient;
 memset(&socketArr,0,sizeof(socketLen));
    // 检查 Winsock 版本号,WSAData为WSADATA结构对象
 WSADATA wsaData;
 int iResult = WSAStartup(MAKEWORD(2,2), &wsaData);
 if (iResult != NO_ERROR)
 {
  printf("Error at WSAStartup()/n");
  return;
 }
 //创建套接字
 SOCKET ListenSocket;
 ListenSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
 if (ListenSocket == INVALID_SOCKET) {
  printf("Error at socket(): %ld/n", WSAGetLastError());
  WSACleanup();
  return;
 }
 //填充sockaddr_in结构
 sockaddr_in service;
 service.sin_family = AF_INET;
 service.sin_addr.S_un.S_addr = inet_addr("127.0.0.1");
    service.sin_port = htons(27015);
 //绑定这个套接字到一个本地地址
 if (bind( ListenSocket,(SOCKADDR*)&service,sizeof(service)) == SOCKET_ERROR)
 {
  printf("bind() failed./n");
  closesocket(ListenSocket);
  WSACleanup();
  return;
 }
 // 进入监听模式
 if (listen( ListenSocket, BACKLOG ) == SOCKET_ERROR)
 {
  printf("Error listening on socket./n");
        closesocket(ListenSocket);
  WSACleanup();
  return ;
 }
 printf("listening...../n");
 while(true)
 {
        //循环接收连接上来的客户端
  sClient = accept(ListenSocket,(SOCKADDR*)&remoteAddr,&nAddrLen);
  if(sClient == INVALID_SOCKET)
  {
   printf("Failed accept!/n");
   continue;
  }
  //else
  //{
  // int bytesRecv = 0;
  // //创建一个接收的结构体,用于接收并解析
  // Header *recvHeader = (Header *)malloc(HEADER_LEN);
  // char* buffer;
  // buffer = (char *)malloc(BUFFER_MAX_SIZE);
  // bytesRecv = recv(sClient,(char *)recvHeader,HEADER_LEN,0);
  // if(bytesRecv OP = ntohl(recvHeader->OP);
  // recvHeader->size = ntohl(recvHeader->size);
  // printf("recv HeaderInfo: OP: %d ,size : %d /n",recvHeader->OP,recvHeader->size);
  // if(recvHeader->size > 0 && recvHeader->size < BUFFER_MAX_SIZE)
  // {
  //  bytesRecv = recv(sClient,buffer,recvHeader->size,0);
  //  if(bytesRecv OP,buffer,recvHeader->size);
  // }
  //}
  //====================================
  host = gethostbyaddr((char *)&remoteAddr.sin_addr.s_addr,4,AF_INET);
  printf("/nClient(%s:%s)connect with server!/n", host->h_name, inet_ntoa(remoteAddr.sin_addr));
  //====================================
  if(socketLen > BACKLOG)
  {
   printf("go beyond the limit 10!/n");
   sClient = NULL;
   break;
  }     
        //创建线程
  HANDLE hThread;
  socketArr[socketLen] = sClient;
  hThread = (HANDLE)_beginthreadex(NULL,0,SockThread,(void *)sClient,0,NULL);
        //用于存储socket的数组下标++
  socketLen++;
 }
    closesocket(ListenSocket);
 WSACleanup();
 return;
 }

//接发数据
unsigned __stdcall SockThread(void *socket)
{
  int cnInt = socketLen - 1;

  int bytesRecv = SOCKET_ERROR;
  Header *recvHeader = (Header *)malloc(HEADER_LEN);
  char*  buffer;
  //char*  tmpBuf;
  char* clientIP;
  struct sockaddr_in client_message;
  int  client_len = sizeof(struct sockaddr_in);
  int dataLen;
  //==================获得连接上来的客户端信息===================
 bytesRecv = getsockname((SOCKET)socket,(struct sockaddr *)&client_message,&client_len);
 clientIP = inet_ntoa(client_message.sin_addr);
 //==================获得连接上来的客户端信息===================
 //暂存接收的数据
 buffer = (char *)malloc(BUFFER_MAX_SIZE);
 while(1)
 {
  bytesRecv = recv((SOCKET)socket,(char*)recvHeader,HEADER_LEN,0);
  if(bytesRecv OP = ntohl(recvHeader->OP);
  recvHeader->size = ntohl(recvHeader->size);
  printf("/n=======recv command:%d ; dataLen : %d./n",recvHeader->OP,recvHeader->size);

  if(recvHeader->size > 0 && recvHeader->size < BUFFER_MAX_SIZE)
  {
   bytesRecv = recv((SOCKET)socket,buffer,recvHeader->size,0);
   if(bytesRecv OP == 0 || recvHeader->OP == 1)
   {
    for(int i = 0 ; i < socketLen; i++)
    {
     if((SOCKET)socket != socketArr[i])
     {
      int dataLen = recvHeader->size;
      recvHeader->OP = htonl(recvHeader->OP);
      recvHeader->size = htonl(recvHeader->size);
      send((SOCKET)socketArr[i],(char *)recvHeader,HEADER_LEN,0);
      if((send((SOCKET)socketArr[i],buffer,dataLen,0)) < 0 )
      {
       printf("server: client(%s) disconnect with server!/n",clientIP);
       break;
      }
     }
    }   
   }
   else
   {
    printf("Send OP error.you must select it which is 0 or 1/n");
   }
            //===============send to other client end===================
   //buffer中仅有dataInfo没有headerInfo
   convertDataAndPrint(recvHeader->OP,buffer,recvHeader->size);
  }
  else
  {
   if(recvHeader->size > 0)
   {
    printf("The client (%s) send the dataLen(%d) larger buffer size(%d).please input small again!/n",
     clientIP,recvHeader->size,BUFFER_MAX_SIZE);
   }
   else
   {
    printf("The client (%s) send the dataLen(%d) less than 0./ndisconnection with server/n",
     clientIP,recvHeader->size);
    //break;
   }
  }
  //memset(buffer,0,recvHeader->size);
 }
  free(buffer);
 _endthreadex( 0 );
 return 0;
}


    
 
 

您可能感兴趣的文章:

  • andriod下java socket网络编程:java socket客户端服务端代码示例
  • 输出java进程的jstack信息示例分享 通过线程堆栈信息分析java线程
  • java Servlet实现Session创建存取以及url重写代码示例
  • java 四舍五入使java保留2位小数示例讲解
  • java进行error捕获和处理示例(java异常捕获)
  • java去除集合中重复元素示例分享 java去除重复
  • java读取csv文件示例分享(java解析csv文件)
  • java求三个数的最大值的示例分享
  • java生成字母数字组合的随机数示例 java生成随机数
  • java实现网页解析示例
  • java协变返回类型使用示例
  • 使用java执行定时任务示例
  • java自定义枚举转换器示例
  • java向文件末尾添加内容示例分享
  • java正则表达式获取url的host示例
  • java使用正则表达校验手机号码示例(手机号码正则)
  • java实现jframe透明窗体示例
  • java的split方法使用示例
  • java抓取网页数据示例
  • Oracle 使用Java Source 简单示例
  • java自定义日期转化类示例
  • VB编写的DLL不能被JAVA调用,用C将VB编写的DLL包装起来,做成一个新的DLL;用C编写的DLL可以被JAVA调用,用JAVA再进行一层的包装,因为JAV
  • 请问从一个java程序中如何调用另一java程序,并控制可以其状态(最小化、最大化、结束程序),还有怎样知道那个被调用的程序结束。
  • 可不可以用java调用其他语言的编译结果?(好象asp调用com那样)
  • 请问如何在Java中调用一个VB所写的DLL?如何调用这个Dll的方法?如何取得和设置这个Dll的属性值?谢谢!
  • ###java调用COM组件或COM调用java组件(EJB)解决办法。###
  • java线程怎么调用java的application.class?
  • 求救!!如何在java程序中调用外部非java的exe文件?
  • 急!请问有分析java程序性能瓶颈的工具吗?例如,统计 java 程序中函数调用次数?
  • java.awt 和java.util都有List,想调用awt的List而出现Ambiguous class错误怎么办?
  • 一个.java文件中怎样来调用另一个.java文件中的变量及其它东西!
  •  
    本站(WWW.)旨在分享和传播互联网科技相关的资讯和技术,将尽最大努力为读者提供更好的信息聚合和浏览方式。
    本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。












  • 相关文章推荐
  • java操作excel2007文档介绍及代码例子
  • 寻找<<java2图形设计卷2SWING>>一书源代码和<<java网络高级编程>>一书源代码
  • java实现判断字符串是否全是数字的四种方法代码举例
  • 怎样将标准的C++代码转换成JAVA代码??
  • java Servlet获取和设置cookie实例代码
  • 哪位会使用代码保护工具WingGuard来保护java代码?
  • Java代码分享工具 Java Gems
  • 各路JAVA高手们,能否给我一个用JAVA写的简单聊天室代码?
  • 你最喜欢去的JAVA网站或JAVA源代码下载网站是哪里???
  • JAVA APPLET与JSP有什么区别?好像都是把JAVA代码嵌到网页中。
  • java里有什么函数可以检查 java 代码并执行它?
  • 谁有Java源代码中floatToIntBits,intBitsToFloat的源代码?
  • 怎样看到java程序经过编译后的代码内容(bytecode的)或者在bytecode在JVM执行时JVM所解析的代码
  • 大哥大姐们小弟刚学JAVA,对它没点头绪啊!能告诉我JAVA在什么环境下编写代码和编译吗??
  • java与js代码互调示例代码
  • java文件复制代码片断(java实现文件拷贝)
  • 你认为最好的中文JAVA网站或有大量优秀JAVA源代码免费下载的网站是哪里???送分!!!
  • 有没有这样的软件:把一个不标准格式的JAVA原代码转换为具有标准(或比较标准)编码规范的代码。
  • 请问在java多线程中,是只有run(){}内的代码运行在一个新线程下呢?还是这个类中的代码都运行在一个新线程下?
  • 决定学Java,有没有小而好用的Java编辑器写代码,就象TurboC一样?
  • 刚学习Java,请教关于Java的Application代码编写问题。
  • java命名空间java.sql类types的类成员方法: java_object定义及介绍
  • 我想学JAVA ,是买THINK IN JAVA 还是JAVA2核心技术:卷1 好???
  • java命名空间java.awt.datatransfer类dataflavor的类成员方法: imageflavor定义及介绍
  • 请问Java高手,Java的优势在那里??,Java主要适合于开发哪类应用程序
  • java命名空间java.lang.management类managementfactory的类成员方法: getcompilationmxbean定义及介绍
  • 如何将java.util.Date转化为java.sql.Date?数据库中Date类型对应于java的哪个Date呢
  • java命名空间java.lang.management接口runtimemxbean的类成员方法: getlibrarypath定义及介绍
  • 谁有电子版的《Java编程思想第二版(Thinking in java second)》和《Java2编程详解(special edition java2)》?得到给分
  • java命名空间java.lang.management接口runtimemxbean的类成员方法: getstarttime定义及介绍
  • 本人想学java,请问java程序员的待遇如何,和java主要有几个比较强的方向


  • 站内导航:


    特别声明:169IT网站部分信息来自互联网,如果侵犯您的权利,请及时告知,本站将立即删除!

    ©2012-2021,,E-mail:www_#163.com(请将#改为@)

    浙ICP备11055608号-3