需要引入ODSocket 类,见附件。
客户端代码如下:
ODSocket cSocket;
cSocket.Init();
cSocket.Create(AF_INET,SOCK_STREAM,0);
cSocket.Connect("192.168.8.145",9443);
char recvBuf[1024] = "\0";
std::string testmsg="adfasdf";
#if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32)
GBKToUTF8(testmsg,"gb2312","utf-8");
#endif
cSocket.Send(testmsg.c_str(),strlen(testmsg.c_str())+1,1);
cSocket.Recv(recvBuf,1024,0);
std::string rec_msg=std::string(recvBuf);
#if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32)
GBKToUTF8(rec_msg,"utf-8","gb2312");
#endif
CCMessageBox(rec_msg.c_str(),"recived data is:");
cSocket.Close();
cSocket.Clean();
#if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32)
//字符转换,使cocos2d-x在win32平台支持中文显示
int AppDelegate::GBKToUTF8(std::string &gbkStr,const char* toCode,const char* formCode)
{
iconv_t iconvH;
iconvH = iconv_open(formCode,toCode);
if(iconvH == 0)
{
return -1;
}
const char* strChar = gbkStr.c_str();
const char** pin = &strChar;
size_t strLength = gbkStr.length();
char* outbuf = (char*)malloc(strLength*4);
char* pBuff = outbuf;
memset(outbuf,0,strLength*4);
size_t outLength = strLength*4;
if(-1 == iconv(iconvH,pin,&strLength,&outbuf,&outLength))
{
iconv_close(iconvH);
return -1;
}
gbkStr = pBuff;
iconv_close(iconvH);
return 0;
}
#endif
服务端代码:
package com.game.server;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
/**
* 主程序
*
* @author Administrator
*
*/
public class MainServer {
public static void main(String[] args) {
ServerSocket ss = null;
try {
ss = new ServerSocket(9443);
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("服务器开启..........");
while (true) {
try {
Socket so = ss.accept();
if (so == null) {
continue;
}
System.out.println("接受到一个请求。。。");
new DoSocket(so).start();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
package com.game.server;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
class DoSocket extends Thread {
private Socket so;
private DataInputStream dis;
private DataOutputStream dos;
private InputStream is;
private OutputStream os;
public DoSocket(Socket so) throws IOException {
this.so = so;
is=so.getInputStream();
os=so.getOutputStream();
}
@Override
public void run() {
while (true) {
try {
ByteArrayOutputStream out = new ByteArrayOutputStream();
int len=is.available();
byte[] buff=new byte[len];
System.out.println("len="+len);
is.read(buff);
out.write(buff, 0, len);
String read =new String(out.toByteArray(),"UTF-8");
System.out.println("接受到的信息:" + read);
String send = "您已经连接上了。";
os.write(send.getBytes("UTF-8"));
break;
} catch (IOException e) {
e.printStackTrace();
break;
}
}
}
}
//
特别说明:
1.在服务端最好不要用datainputstream,C++代码发送的数据会是乱码。
2.在读流的时候,C++发送的数据,在读到最后一个数据后,流不会返回-1结束标志,所有我这里就没有用while循环来读流。
3.传说ODSocket这个类,已经实现了跨平台,不用第三方包,本人只在win32下测试通过,在android上没有测试。
客户端代码如下:
ODSocket cSocket;
cSocket.Init();
cSocket.Create(AF_INET,SOCK_STREAM,0);
cSocket.Connect("192.168.8.145",9443);
char recvBuf[1024] = "\0";
std::string testmsg="adfasdf";
#if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32)
GBKToUTF8(testmsg,"gb2312","utf-8");
#endif
cSocket.Send(testmsg.c_str(),strlen(testmsg.c_str())+1,1);
cSocket.Recv(recvBuf,1024,0);
std::string rec_msg=std::string(recvBuf);
#if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32)
GBKToUTF8(rec_msg,"utf-8","gb2312");
#endif
CCMessageBox(rec_msg.c_str(),"recived data is:");
cSocket.Close();
cSocket.Clean();
#if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32)
//字符转换,使cocos2d-x在win32平台支持中文显示
int AppDelegate::GBKToUTF8(std::string &gbkStr,const char* toCode,const char* formCode)
{
iconv_t iconvH;
iconvH = iconv_open(formCode,toCode);
if(iconvH == 0)
{
return -1;
}
const char* strChar = gbkStr.c_str();
const char** pin = &strChar;
size_t strLength = gbkStr.length();
char* outbuf = (char*)malloc(strLength*4);
char* pBuff = outbuf;
memset(outbuf,0,strLength*4);
size_t outLength = strLength*4;
if(-1 == iconv(iconvH,pin,&strLength,&outbuf,&outLength))
{
iconv_close(iconvH);
return -1;
}
gbkStr = pBuff;
iconv_close(iconvH);
return 0;
}
#endif
服务端代码:
package com.game.server;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
/**
* 主程序
*
* @author Administrator
*
*/
public class MainServer {
public static void main(String[] args) {
ServerSocket ss = null;
try {
ss = new ServerSocket(9443);
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("服务器开启..........");
while (true) {
try {
Socket so = ss.accept();
if (so == null) {
continue;
}
System.out.println("接受到一个请求。。。");
new DoSocket(so).start();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
package com.game.server;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
class DoSocket extends Thread {
private Socket so;
private DataInputStream dis;
private DataOutputStream dos;
private InputStream is;
private OutputStream os;
public DoSocket(Socket so) throws IOException {
this.so = so;
is=so.getInputStream();
os=so.getOutputStream();
}
@Override
public void run() {
while (true) {
try {
ByteArrayOutputStream out = new ByteArrayOutputStream();
int len=is.available();
byte[] buff=new byte[len];
System.out.println("len="+len);
is.read(buff);
out.write(buff, 0, len);
String read =new String(out.toByteArray(),"UTF-8");
System.out.println("接受到的信息:" + read);
String send = "您已经连接上了。";
os.write(send.getBytes("UTF-8"));
break;
} catch (IOException e) {
e.printStackTrace();
break;
}
}
}
}
//
特别说明:
1.在服务端最好不要用datainputstream,C++代码发送的数据会是乱码。
2.在读流的时候,C++发送的数据,在读到最后一个数据后,流不会返回-1结束标志,所有我这里就没有用while循环来读流。
3.传说ODSocket这个类,已经实现了跨平台,不用第三方包,本人只在win32下测试通过,在android上没有测试。