Erwan
- Crude networking functions: + Basic message works on Windows + Compiles on PSP but doesn't work yet + Nothing done for Linux -> TODO!
This commit is contained in:
@@ -388,6 +388,10 @@
|
|||||||
RelativePath=".\src\JMD2Model.cpp"
|
RelativePath=".\src\JMD2Model.cpp"
|
||||||
>
|
>
|
||||||
</File>
|
</File>
|
||||||
|
<File
|
||||||
|
RelativePath=".\src\JNetwork.cpp"
|
||||||
|
>
|
||||||
|
</File>
|
||||||
<File
|
<File
|
||||||
RelativePath=".\src\JOBJModel.cpp"
|
RelativePath=".\src\JOBJModel.cpp"
|
||||||
>
|
>
|
||||||
@@ -416,6 +420,10 @@
|
|||||||
RelativePath=".\src\JResourceManager.cpp"
|
RelativePath=".\src\JResourceManager.cpp"
|
||||||
>
|
>
|
||||||
</File>
|
</File>
|
||||||
|
<File
|
||||||
|
RelativePath=".\src\win\JSocket_Win.cpp"
|
||||||
|
>
|
||||||
|
</File>
|
||||||
<File
|
<File
|
||||||
RelativePath=".\src\win\JSoundSystem_Win.cpp"
|
RelativePath=".\src\win\JSoundSystem_Win.cpp"
|
||||||
>
|
>
|
||||||
@@ -583,6 +591,10 @@
|
|||||||
RelativePath=".\include\JMD2Model.h"
|
RelativePath=".\include\JMD2Model.h"
|
||||||
>
|
>
|
||||||
</File>
|
</File>
|
||||||
|
<File
|
||||||
|
RelativePath=".\include\JNetwork.h"
|
||||||
|
>
|
||||||
|
</File>
|
||||||
<File
|
<File
|
||||||
RelativePath=".\include\JOBJModel.h"
|
RelativePath=".\include\JOBJModel.h"
|
||||||
>
|
>
|
||||||
@@ -611,6 +623,10 @@
|
|||||||
RelativePath="include\JResourceManager.h"
|
RelativePath="include\JResourceManager.h"
|
||||||
>
|
>
|
||||||
</File>
|
</File>
|
||||||
|
<File
|
||||||
|
RelativePath=".\include\JSocket.h"
|
||||||
|
>
|
||||||
|
</File>
|
||||||
<File
|
<File
|
||||||
RelativePath=".\include\JSoundSystem.h"
|
RelativePath=".\include\JSoundSystem.h"
|
||||||
>
|
>
|
||||||
|
|||||||
@@ -7,6 +7,7 @@ GENERIC_OBJS = src/JApp.o src/JGBKFont.o \
|
|||||||
src/JGE.o src/JGui.o src/JLBFont.o \
|
src/JGE.o src/JGui.o src/JLBFont.o \
|
||||||
src/JGameObject.o src/JSpline.o src/JAnimator.o \
|
src/JGameObject.o src/JSpline.o src/JAnimator.o \
|
||||||
src/JResourceManager.o src/JFileSystem.o \
|
src/JResourceManager.o src/JFileSystem.o \
|
||||||
|
src/JNetwork.o src/JSocket.o \
|
||||||
src/JParticle.o src/JParticleEmitter.o src/JParticleEffect.o \
|
src/JParticle.o src/JParticleEmitter.o src/JParticleEffect.o \
|
||||||
src/JParticleSystem.o \
|
src/JParticleSystem.o \
|
||||||
src/unzip/ioapi.o src/unzip/mztools.o src/unzip/unzip.o \
|
src/unzip/ioapi.o src/unzip/mztools.o src/unzip/unzip.o \
|
||||||
|
|||||||
43
JGE/include/JNetwork.h
Normal file
43
JGE/include/JNetwork.h
Normal file
@@ -0,0 +1,43 @@
|
|||||||
|
#ifndef _JNETWORK_H_
|
||||||
|
#define _JNETWORK_H_
|
||||||
|
|
||||||
|
#ifdef WIN32
|
||||||
|
#elif defined (LINUX)
|
||||||
|
#else
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#include "JGE.h"
|
||||||
|
#include <string>
|
||||||
|
using namespace std;
|
||||||
|
|
||||||
|
class JNetwork{
|
||||||
|
private:
|
||||||
|
static JNetwork * mInstance;
|
||||||
|
static int connected_to_ap;
|
||||||
|
public:
|
||||||
|
JNetwork();
|
||||||
|
static JNetwork * GetInstance();
|
||||||
|
static void EndInstance();
|
||||||
|
static string serverIP;
|
||||||
|
int receive(char * buffer, int length);
|
||||||
|
int send(char * buffer, int length);
|
||||||
|
int connect(string serverIP = "");
|
||||||
|
static int isConnected();
|
||||||
|
#if defined (WIN32) || defined (LINUX)
|
||||||
|
static int net_thread(LPVOID param);
|
||||||
|
#else
|
||||||
|
static int net_thread(SceSize args, void *argp);
|
||||||
|
static int connect_to_apctl(int config);
|
||||||
|
#endif
|
||||||
|
|
||||||
|
private:
|
||||||
|
|
||||||
|
#if defined (WIN32) || defined (LINUX)
|
||||||
|
static DWORD netthread;
|
||||||
|
#else
|
||||||
|
static int netthread;
|
||||||
|
#endif
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif
|
||||||
30
JGE/include/JSocket.h
Normal file
30
JGE/include/JSocket.h
Normal file
@@ -0,0 +1,30 @@
|
|||||||
|
#ifndef _JSOCKET_H_
|
||||||
|
#define _JSOCKET_H_
|
||||||
|
|
||||||
|
#include <queue>
|
||||||
|
using namespace std;
|
||||||
|
|
||||||
|
//TODO config ?
|
||||||
|
#define SERVER_PORT 20666
|
||||||
|
|
||||||
|
class JSocket{
|
||||||
|
public:
|
||||||
|
queue<char> received_data;
|
||||||
|
queue<char> tosend_data;
|
||||||
|
static JSocket * mInstance;
|
||||||
|
int start_server(const char *szIpAddr);
|
||||||
|
int start_client(const char *szIpAddr);
|
||||||
|
|
||||||
|
JSocket();
|
||||||
|
~JSocket();
|
||||||
|
static int connected;
|
||||||
|
private:
|
||||||
|
void init();
|
||||||
|
void readWrite(int sock);
|
||||||
|
#if defined (WIN32) || defined (LINUX)
|
||||||
|
#else
|
||||||
|
int make_socket(uint16_t port);
|
||||||
|
#endif
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif
|
||||||
213
JGE/src/JNetwork.cpp
Normal file
213
JGE/src/JNetwork.cpp
Normal file
@@ -0,0 +1,213 @@
|
|||||||
|
/*
|
||||||
|
PSP Network function calls
|
||||||
|
|
||||||
|
|
||||||
|
*/
|
||||||
|
|
||||||
|
#if defined (WIN32) || defined (LINUX)
|
||||||
|
#else
|
||||||
|
#include <pspkernel.h>
|
||||||
|
#include <pspdebug.h>
|
||||||
|
#include <pspsdk.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
#include <pspnet.h>
|
||||||
|
#include <pspnet_inet.h>
|
||||||
|
#include <pspnet_apctl.h>
|
||||||
|
#include <pspnet_resolver.h>
|
||||||
|
#include <netinet/in.h>
|
||||||
|
#include <arpa/inet.h>
|
||||||
|
#include <sys/select.h>
|
||||||
|
#include <errno.h>
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#include "../include/JNetwork.h"
|
||||||
|
#include "../include/JSocket.h"
|
||||||
|
|
||||||
|
JNetwork* JNetwork::mInstance = NULL;
|
||||||
|
string JNetwork::serverIP = "";
|
||||||
|
|
||||||
|
JNetwork * JNetwork::GetInstance(){
|
||||||
|
if (!mInstance) mInstance = new JNetwork();
|
||||||
|
return mInstance;
|
||||||
|
}
|
||||||
|
|
||||||
|
void JNetwork::EndInstance(){
|
||||||
|
SAFE_DELETE(mInstance);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
#if defined (WIN32) || defined (LINUX)
|
||||||
|
DWORD JNetwork::netthread = 0;
|
||||||
|
int JNetwork::connected_to_ap = 1;
|
||||||
|
#else
|
||||||
|
int JNetwork::connected_to_ap = 0;
|
||||||
|
int JNetwork::netthread = 0;
|
||||||
|
#endif
|
||||||
|
|
||||||
|
int JNetwork::isConnected(){
|
||||||
|
if (connected_to_ap !=1) return 0;
|
||||||
|
return JSocket::connected;
|
||||||
|
}
|
||||||
|
|
||||||
|
JNetwork::JNetwork(){
|
||||||
|
}
|
||||||
|
|
||||||
|
int JNetwork::receive(char * buffer, int length){
|
||||||
|
JSocket * socket = JSocket::mInstance;
|
||||||
|
if (!socket) return 0;
|
||||||
|
int size = 0;
|
||||||
|
while(!socket->received_data.empty() && size < length){
|
||||||
|
buffer[size] = socket->received_data.front();
|
||||||
|
socket->received_data.pop();
|
||||||
|
size++;
|
||||||
|
}
|
||||||
|
return size;
|
||||||
|
}
|
||||||
|
|
||||||
|
int JNetwork::send(char * buffer, int length){
|
||||||
|
JSocket * socket = JSocket::mInstance;
|
||||||
|
if (!socket) return 0;
|
||||||
|
for (int i = 0; i < length; i++){
|
||||||
|
socket->tosend_data.push(buffer[i]);
|
||||||
|
}
|
||||||
|
return length;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
#ifdef WIN32
|
||||||
|
int JNetwork::net_thread(LPVOID param)
|
||||||
|
{
|
||||||
|
do
|
||||||
|
{
|
||||||
|
JSocket::mInstance = new JSocket();
|
||||||
|
JSocket * s = JSocket::mInstance;
|
||||||
|
if (JNetwork::serverIP.size()){
|
||||||
|
OutputDebugString(JNetwork::serverIP.c_str());
|
||||||
|
s->start_client(JNetwork::serverIP.c_str());
|
||||||
|
|
||||||
|
}else{
|
||||||
|
s->start_server(""); //IP address useless for server ?
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
while(0);
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
int JNetwork::connect(string serverIP){
|
||||||
|
if(netthread) return 0;
|
||||||
|
|
||||||
|
JNetwork::serverIP = serverIP;
|
||||||
|
/* Create a user thread to do the real work */
|
||||||
|
HANDLE hthread;
|
||||||
|
hthread=CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)net_thread,0,0,&netthread);
|
||||||
|
return netthread;
|
||||||
|
}
|
||||||
|
|
||||||
|
#else
|
||||||
|
int net_thread(SceSize args, void *argp)
|
||||||
|
{
|
||||||
|
|
||||||
|
|
||||||
|
do
|
||||||
|
{
|
||||||
|
JSocket::mInstance = new JSocket();
|
||||||
|
JSocket * s = JSocket::mInstance;
|
||||||
|
if (JNetwork::serverIP.size()){
|
||||||
|
s->start_client(JNetwork::serverIP.c_str());
|
||||||
|
|
||||||
|
}else{
|
||||||
|
// connected, get my IPADDR and run test
|
||||||
|
SceNetApctlInfo szMyIPAddr;
|
||||||
|
if (sceNetApctlGetInfo(8, &szMyIPAddr) != 0){
|
||||||
|
}else{
|
||||||
|
s->start_server(szMyIPAddr.ip);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
while(0);
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
int JNetwork::connect(string serverIP){
|
||||||
|
int err;
|
||||||
|
if(netthread) return 0;
|
||||||
|
if((err = pspSdkInetInit())){
|
||||||
|
printf("JGE Error, could not initialise the network %08X\n", err);
|
||||||
|
return err;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(JNetwork::connect_to_apctl(1)){
|
||||||
|
JNetwork::serverIP = serverIP;
|
||||||
|
/* Create a user thread to do the real work */
|
||||||
|
netthread = sceKernelCreateThread("net_thread", net_thread, 0x18, 0x10000, PSP_THREAD_ATTR_USER, NULL);
|
||||||
|
|
||||||
|
if(netthread < 0)
|
||||||
|
{
|
||||||
|
printf("Error, could not create thread\n");
|
||||||
|
sceKernelSleepThread();
|
||||||
|
}
|
||||||
|
|
||||||
|
sceKernelStartThread(netthread, 0, NULL);
|
||||||
|
return netthread;
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/* Connect to an access point */
|
||||||
|
int JNetwork::connect_to_apctl(int config)
|
||||||
|
{
|
||||||
|
int err;
|
||||||
|
int stateLast = -1;
|
||||||
|
|
||||||
|
/* Connect using the first profile */
|
||||||
|
err = sceNetApctlConnect(config);
|
||||||
|
if (err != 0)
|
||||||
|
{
|
||||||
|
printf("JGE: sceNetApctlConnect returns %08X\n", err);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
printf("JGE: Connecting...\n");
|
||||||
|
while (1)
|
||||||
|
{
|
||||||
|
int state;
|
||||||
|
err = sceNetApctlGetState(&state);
|
||||||
|
if (err != 0)
|
||||||
|
{
|
||||||
|
printf("JGE: sceNetApctlGetState returns $%x\n", err);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
if (state > stateLast)
|
||||||
|
{
|
||||||
|
printf(" connection state %d of 4\n", state);
|
||||||
|
stateLast = state;
|
||||||
|
}
|
||||||
|
if (state == 4){
|
||||||
|
connected_to_ap = 1;
|
||||||
|
break; // connected with static IP
|
||||||
|
}
|
||||||
|
// wait a little before polling again
|
||||||
|
sceKernelDelayThread(50*1000); // 50ms
|
||||||
|
}
|
||||||
|
printf("JGE: Connected!\n");
|
||||||
|
|
||||||
|
if(err != 0)
|
||||||
|
{
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
#endif
|
||||||
211
JGE/src/JSocket.cpp
Normal file
211
JGE/src/JSocket.cpp
Normal file
@@ -0,0 +1,211 @@
|
|||||||
|
#include <pspkernel.h>
|
||||||
|
#include <pspdebug.h>
|
||||||
|
#include <pspdisplay.h>
|
||||||
|
#include <pspsdk.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
#include <pspnet.h>
|
||||||
|
#include <pspnet_inet.h>
|
||||||
|
#include <pspnet_apctl.h>
|
||||||
|
#include <pspnet_resolver.h>
|
||||||
|
#include <psputility_netmodules.h>
|
||||||
|
#include <netinet/in.h>
|
||||||
|
#include <arpa/inet.h>
|
||||||
|
#include <sys/select.h>
|
||||||
|
#include <errno.h>
|
||||||
|
|
||||||
|
#include "../include/JGE.h"
|
||||||
|
|
||||||
|
#include "../include/JSocket.h"
|
||||||
|
JSocket * JSocket::mInstance = NULL;
|
||||||
|
|
||||||
|
#define SERVER_PORT 20666
|
||||||
|
|
||||||
|
void JSocket::init(){
|
||||||
|
sceUtilityLoadNetModule(1);
|
||||||
|
sceUtilityLoadNetModule(3);
|
||||||
|
}
|
||||||
|
|
||||||
|
JSocket::JSocket(){
|
||||||
|
init();
|
||||||
|
}
|
||||||
|
|
||||||
|
JSocket::~JSocket(){
|
||||||
|
pspSdkInetTerm();
|
||||||
|
sceNetApctlDisconnect();
|
||||||
|
sceNetApctlTerm();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
int JSocket::make_socket(uint16_t port)
|
||||||
|
{
|
||||||
|
int sock;
|
||||||
|
int ret;
|
||||||
|
struct sockaddr_in name;
|
||||||
|
|
||||||
|
sock = socket(PF_INET, SOCK_STREAM, 0);
|
||||||
|
if(sock < 0)
|
||||||
|
{
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
name.sin_family = AF_INET;
|
||||||
|
name.sin_port = htons(port);
|
||||||
|
name.sin_addr.s_addr = htonl(INADDR_ANY);
|
||||||
|
ret = bind(sock, (struct sockaddr *) &name, sizeof(name));
|
||||||
|
if(ret < 0)
|
||||||
|
{
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
return sock;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void JSocket::readWrite(int val){
|
||||||
|
char data[1024];
|
||||||
|
int readbytes;
|
||||||
|
readbytes = read(val, data, sizeof(data));
|
||||||
|
if(readbytes < 0)
|
||||||
|
{
|
||||||
|
printf("Socket %d closed\n", val);
|
||||||
|
close(val);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
for (int i = 0; i < readbytes; i++){
|
||||||
|
received_data.push(data[i]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//Write
|
||||||
|
unsigned int size = 0;
|
||||||
|
while(!tosend_data.empty()){
|
||||||
|
size++;
|
||||||
|
if (size > sizeof(data)){
|
||||||
|
write(val,data,sizeof(data));
|
||||||
|
size = 0;
|
||||||
|
}
|
||||||
|
data[size-1] = tosend_data.front();
|
||||||
|
tosend_data.pop();
|
||||||
|
}
|
||||||
|
if (size) write(val,data,size);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Start a client */
|
||||||
|
int JSocket::start_client(const char *szIpAddr){
|
||||||
|
int sock;
|
||||||
|
sockaddr_in addrListen;
|
||||||
|
int error;
|
||||||
|
|
||||||
|
sock = sceNetInetSocket(AF_INET, SOCK_STREAM, 0);
|
||||||
|
if (sock <= 0){
|
||||||
|
printf("socket returned $%x\n", sock);
|
||||||
|
sceKernelDelayThread(500*1000);
|
||||||
|
return sock;
|
||||||
|
}
|
||||||
|
|
||||||
|
addrListen.sin_family = AF_INET;
|
||||||
|
addrListen.sin_addr.s_addr = inet_addr(szIpAddr);
|
||||||
|
addrListen.sin_port = htons(SERVER_PORT);
|
||||||
|
|
||||||
|
int err = sceNetInetConnect(sock, (sockaddr *)&addrListen, sizeof(addrListen));
|
||||||
|
if (err != 0){
|
||||||
|
printf("Unable to connect!\n");
|
||||||
|
printf("connect returned $%x\n", err);
|
||||||
|
printf(" errno=$%x\n", sceNetInetGetErrno());
|
||||||
|
sceKernelDelayThread(500*1000);
|
||||||
|
return err;
|
||||||
|
}
|
||||||
|
|
||||||
|
while(1){
|
||||||
|
readWrite(sock);
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Start a server */
|
||||||
|
int JSocket::start_server(const char *szIpAddr)
|
||||||
|
{
|
||||||
|
int ret;
|
||||||
|
int sock;
|
||||||
|
int _new = -1;
|
||||||
|
struct sockaddr_in client;
|
||||||
|
size_t size;
|
||||||
|
int readbytes;
|
||||||
|
|
||||||
|
fd_set set;
|
||||||
|
fd_set setsave;
|
||||||
|
|
||||||
|
/* Create a socket for listening */
|
||||||
|
sock = make_socket(SERVER_PORT);
|
||||||
|
if(sock < 0)
|
||||||
|
{
|
||||||
|
printf("Error creating server socket\n");
|
||||||
|
return sock;
|
||||||
|
}
|
||||||
|
|
||||||
|
ret = listen(sock, 1);
|
||||||
|
if(ret < 0)
|
||||||
|
{
|
||||||
|
printf("Error calling listen\n");
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
printf("Listening for connections ip %s port %d\n", szIpAddr, SERVER_PORT);
|
||||||
|
|
||||||
|
FD_ZERO(&set);
|
||||||
|
FD_SET(sock, &set);
|
||||||
|
setsave = set;
|
||||||
|
|
||||||
|
while(1)
|
||||||
|
{
|
||||||
|
int i;
|
||||||
|
set = setsave;
|
||||||
|
if(select(FD_SETSIZE, &set, NULL, NULL, NULL) < 0)
|
||||||
|
{
|
||||||
|
printf("select error\n");
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
for(i = 0; i < FD_SETSIZE; i++)
|
||||||
|
{
|
||||||
|
if(FD_ISSET(i, &set))
|
||||||
|
{
|
||||||
|
int val = i;
|
||||||
|
|
||||||
|
if(val == sock)
|
||||||
|
{
|
||||||
|
_new = accept(sock, (struct sockaddr *) &client, &size);
|
||||||
|
if(_new < 0)
|
||||||
|
{
|
||||||
|
printf("Error in accept %s\n", strerror(errno));
|
||||||
|
close(sock);
|
||||||
|
return _new;
|
||||||
|
}
|
||||||
|
|
||||||
|
printf("New connection %d from %s:%d\n", val,
|
||||||
|
inet_ntoa(client.sin_addr),
|
||||||
|
ntohs(client.sin_port));
|
||||||
|
|
||||||
|
write(_new, "123567", strlen("123567"));
|
||||||
|
|
||||||
|
FD_SET(_new, &setsave);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
readWrite(val);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
close(sock);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
230
JGE/src/win/JSocket_Win.cpp
Normal file
230
JGE/src/win/JSocket_Win.cpp
Normal file
@@ -0,0 +1,230 @@
|
|||||||
|
#pragma comment(lib,"WSOCK32.LIB")
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <conio.h>
|
||||||
|
#include <winsock.h>
|
||||||
|
#include <errno.h>
|
||||||
|
#include <winsock.h>
|
||||||
|
|
||||||
|
#include "../../include/JSocket.h"
|
||||||
|
JSocket * JSocket::mInstance = NULL;
|
||||||
|
|
||||||
|
#define SERVER_PORT 20666
|
||||||
|
unsigned char ADRESSE_IP_SERVEUR [4] = {127,0,0,1};
|
||||||
|
|
||||||
|
|
||||||
|
int JSocket::connected = 0;
|
||||||
|
|
||||||
|
void JSocket::init(){
|
||||||
|
//TODO ?
|
||||||
|
}
|
||||||
|
|
||||||
|
JSocket::JSocket(){
|
||||||
|
init();
|
||||||
|
}
|
||||||
|
|
||||||
|
JSocket::~JSocket(){
|
||||||
|
//TODO ?
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void JSocket::readWrite(int val){
|
||||||
|
char data[1024];
|
||||||
|
|
||||||
|
fd_set set;
|
||||||
|
FD_ZERO(&set);
|
||||||
|
FD_SET(val, &set);
|
||||||
|
struct timeval tv;
|
||||||
|
int result = select(0, &set, NULL, NULL, &tv);
|
||||||
|
if( result< 0)
|
||||||
|
{
|
||||||
|
printf("Socket %d closed\n", val);
|
||||||
|
closesocket(val);
|
||||||
|
connected= 0;
|
||||||
|
printf("select error\n");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (result > 0){
|
||||||
|
OutputDebugString("Receiving!\n");
|
||||||
|
int readbytes = recv(val, data, sizeof(data),0);
|
||||||
|
if(readbytes < 0)
|
||||||
|
{
|
||||||
|
int error = WSAGetLastError();
|
||||||
|
if (error != WSAEWOULDBLOCK){
|
||||||
|
printf("Socket %d closed\n", val);
|
||||||
|
closesocket(val);
|
||||||
|
connected= 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
OutputDebugString("received Data!!!\n");
|
||||||
|
for (int i = 0; i < readbytes; i++){
|
||||||
|
received_data.push(data[i]);
|
||||||
|
OutputDebugString("getting byte\n");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//Write
|
||||||
|
int size = 0;
|
||||||
|
while(!tosend_data.empty()){
|
||||||
|
size++;
|
||||||
|
if (size > sizeof(data)){
|
||||||
|
send(val,data,sizeof(data),0);
|
||||||
|
size = 0;
|
||||||
|
}
|
||||||
|
data[size-1] = tosend_data.front();
|
||||||
|
tosend_data.pop();
|
||||||
|
}
|
||||||
|
if (size) {
|
||||||
|
send(val,data,size-1,0);
|
||||||
|
OutputDebugString("sending Data\n");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Start a client */
|
||||||
|
int JSocket::start_client(const char *szIpAddr){
|
||||||
|
SOCKET Desc_Socket_Cliente;
|
||||||
|
SOCKADDR_IN Adresse_Socket_Serveur;
|
||||||
|
WORD wVersionRequested;
|
||||||
|
WSADATA wsaData;
|
||||||
|
|
||||||
|
struct hostent *hostentptr;
|
||||||
|
|
||||||
|
wVersionRequested=MAKEWORD(1,1);
|
||||||
|
WSAStartup(wVersionRequested,&wsaData);
|
||||||
|
|
||||||
|
Desc_Socket_Cliente=socket(AF_INET,SOCK_STREAM,0);
|
||||||
|
unsigned int addr_dest = inet_addr(szIpAddr);
|
||||||
|
hostentptr=gethostbyaddr((char*) &addr_dest,4,AF_INET);
|
||||||
|
|
||||||
|
ZeroMemory(&Adresse_Socket_Serveur,sizeof(Adresse_Socket_Serveur));
|
||||||
|
|
||||||
|
Adresse_Socket_Serveur.sin_family=(*hostentptr).h_addrtype;
|
||||||
|
Adresse_Socket_Serveur.sin_port=htons(SERVER_PORT);
|
||||||
|
Adresse_Socket_Serveur.sin_addr=*((struct in_addr*)(*hostentptr).h_addr);
|
||||||
|
|
||||||
|
int result = connect(
|
||||||
|
Desc_Socket_Cliente,
|
||||||
|
(const struct sockaddr*)&Adresse_Socket_Serveur,
|
||||||
|
sizeof(Adresse_Socket_Serveur));
|
||||||
|
|
||||||
|
OutputDebugString("client state 0\n");
|
||||||
|
|
||||||
|
if (result != 0){
|
||||||
|
connected = 0;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
OutputDebugString("client state 1\n");
|
||||||
|
|
||||||
|
|
||||||
|
connected = 1;
|
||||||
|
|
||||||
|
while(1){
|
||||||
|
readWrite(Desc_Socket_Cliente);
|
||||||
|
/* Nb_Caracteres_Recus=recv(Desc_Socket_Cliente,Message_Recu,sizeof(Message_Recu),0);
|
||||||
|
if(Nb_Caracteres_Recus<=0){
|
||||||
|
//continuer=FALSE;
|
||||||
|
}else{
|
||||||
|
strcpy(message,Message_Recu);
|
||||||
|
len=strlen(message);
|
||||||
|
}
|
||||||
|
*/
|
||||||
|
}
|
||||||
|
#
|
||||||
|
closesocket(Desc_Socket_Cliente);
|
||||||
|
WSACleanup();
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Start a server */
|
||||||
|
int JSocket::start_server(const char *szIpAddr){
|
||||||
|
OutputDebugString("server state 0\n");
|
||||||
|
connected= 0;
|
||||||
|
int Code_Retour;
|
||||||
|
SOCKET Desc_Socket_Connection;
|
||||||
|
SOCKADDR_IN Adresse_Socket_Connection;
|
||||||
|
WORD wVersionRequested;
|
||||||
|
WSADATA wsaData;
|
||||||
|
|
||||||
|
wVersionRequested=MAKEWORD(1,1);
|
||||||
|
|
||||||
|
Code_Retour=WSAStartup(wVersionRequested,&wsaData);
|
||||||
|
|
||||||
|
if(Code_Retour!=0){
|
||||||
|
perror("WSAStartup\t");
|
||||||
|
_getch();
|
||||||
|
WSACleanup();
|
||||||
|
return Code_Retour;
|
||||||
|
}
|
||||||
|
OutputDebugString("server state 1\n");
|
||||||
|
/* printf("la version supportee est : %d.%d\n",
|
||||||
|
LOBYTE(wsaData.wHighVersion),
|
||||||
|
HIBYTE(wsaData.wHighVersion)
|
||||||
|
);*/
|
||||||
|
|
||||||
|
Desc_Socket_Connection=socket( AF_INET, SOCK_STREAM,0);
|
||||||
|
|
||||||
|
//printf("valeur de la socket = %d\n",Desc_Socket_Connection);
|
||||||
|
|
||||||
|
ZeroMemory(&Adresse_Socket_Connection,sizeof(Adresse_Socket_Connection));
|
||||||
|
Adresse_Socket_Connection.sin_family=AF_INET;
|
||||||
|
Adresse_Socket_Connection.sin_port=htons(SERVER_PORT);
|
||||||
|
|
||||||
|
Code_Retour=bind(Desc_Socket_Connection,
|
||||||
|
(struct sockaddr*)&Adresse_Socket_Connection,
|
||||||
|
sizeof(Adresse_Socket_Connection));
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
if(Code_Retour!=0){
|
||||||
|
perror("bind\t");
|
||||||
|
_getch();
|
||||||
|
closesocket(Desc_Socket_Connection);
|
||||||
|
WSACleanup();
|
||||||
|
return Code_Retour;
|
||||||
|
}
|
||||||
|
OutputDebugString("server state 3\n");
|
||||||
|
|
||||||
|
Code_Retour=listen(Desc_Socket_Connection,1);
|
||||||
|
if(Code_Retour!=0){
|
||||||
|
perror("listen\n");
|
||||||
|
WSACleanup();
|
||||||
|
return Code_Retour;
|
||||||
|
}
|
||||||
|
OutputDebugString("server state 4\n");
|
||||||
|
printf("serveur en attente d'une connection\n\n");
|
||||||
|
printf("***************arret du serveur par<CTRL><C>**************\n\n");
|
||||||
|
connected = 1;
|
||||||
|
while(1){
|
||||||
|
SOCKET * pt_Nouveau_Socket_Serveur;
|
||||||
|
SOCKADDR_IN Adresse_Socket_Cliente;
|
||||||
|
int Longueur_Adresse;
|
||||||
|
|
||||||
|
pt_Nouveau_Socket_Serveur = new SOCKET;
|
||||||
|
|
||||||
|
Longueur_Adresse = sizeof(Adresse_Socket_Cliente);
|
||||||
|
|
||||||
|
int val=accept(
|
||||||
|
Desc_Socket_Connection,
|
||||||
|
(struct sockaddr*)&Adresse_Socket_Cliente,
|
||||||
|
&Longueur_Adresse);
|
||||||
|
printf("connection accepte depuis le port client %d\n", ntohs(Adresse_Socket_Cliente.sin_port));
|
||||||
|
OutputDebugString("connection accepte depuis le port client\n");
|
||||||
|
|
||||||
|
|
||||||
|
while(1)
|
||||||
|
{
|
||||||
|
readWrite(val);
|
||||||
|
}
|
||||||
|
closesocket(*pt_Nouveau_Socket_Serveur);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
Reference in New Issue
Block a user