mirror of
https://github.com/slsdetectorgroup/slsDetectorPackage.git
synced 2025-06-11 04:17:15 +02:00
SlsDetector client library and servers. First import.
git-svn-id: file:///afs/psi.ch/project/sls_det_software/svn/slsDetectorSoftware@1 951219d9-93cf-4727-9268-0efd64621fa3
This commit is contained in:
BIN
slsDetectorSoftware/MySocketTCP/._.DS_Store
Normal file
BIN
slsDetectorSoftware/MySocketTCP/._.DS_Store
Normal file
Binary file not shown.
23
slsDetectorSoftware/MySocketTCP/Makefile
Normal file
23
slsDetectorSoftware/MySocketTCP/Makefile
Normal file
@ -0,0 +1,23 @@
|
||||
|
||||
TOBECLEANED = MySocketTCP.o
|
||||
|
||||
PROGRAMS = rec send
|
||||
|
||||
all: $(PROGRAMS)
|
||||
|
||||
clean:
|
||||
@rm -f $(TOBECLEANED) $(PROGRAMS)
|
||||
|
||||
rec: MySocketTCP.o rec.cxx
|
||||
g++ -o $@ $^
|
||||
@echo "$@ done"
|
||||
|
||||
send: MySocketTCP.o send.cxx
|
||||
g++ -o $@ $^
|
||||
@echo "$@ done"
|
||||
|
||||
MySocketTCP.o: MySocketTCP.cxx MySocketTCP.h
|
||||
g++ -c $<
|
||||
@echo "$@ done"
|
||||
|
||||
|
1
slsDetectorSoftware/MySocketTCP/MySocketTCP.c
Symbolic link
1
slsDetectorSoftware/MySocketTCP/MySocketTCP.c
Symbolic link
@ -0,0 +1 @@
|
||||
MySocketTCP.cxx
|
231
slsDetectorSoftware/MySocketTCP/MySocketTCP.cxx
Normal file
231
slsDetectorSoftware/MySocketTCP/MySocketTCP.cxx
Normal file
@ -0,0 +1,231 @@
|
||||
|
||||
//version 1.0, ba
|
||||
#include "MySocketTCP.h"
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
using namespace std;
|
||||
//se development ij 19/01/09
|
||||
|
||||
#include <string.h>
|
||||
#include <iostream>
|
||||
#include <math.h>
|
||||
MySocketTCP::~MySocketTCP(){
|
||||
Disconnect();
|
||||
if (socketDescriptor >= 0){
|
||||
close(socketDescriptor);
|
||||
}
|
||||
file_des=-1;
|
||||
}
|
||||
|
||||
|
||||
MySocketTCP::MySocketTCP(unsigned short int const port_number): last_keep_connection_open_action_was_a_send(0), file_des(-1), send_rec_max_size(SEND_REC_MAX_SIZE), is_a_server(1), portno(DEFAULT_PORTNO), socketDescriptor(-1)
|
||||
{ // receiver (server) local no need for ip
|
||||
//is_a_server = 1;
|
||||
|
||||
portno=port_number;
|
||||
strcpy(hostname,"localhost");
|
||||
// SetupParameters();
|
||||
|
||||
socketDescriptor = socket(AF_INET, SOCK_STREAM,0); //tcp
|
||||
|
||||
if (socketDescriptor < 0) {
|
||||
cerr << "Can not create socket "<<endl;
|
||||
} else {
|
||||
|
||||
// Set some fields in the serverAddress structure.
|
||||
serverAddress.sin_family = AF_INET;
|
||||
serverAddress.sin_addr.s_addr = htonl(INADDR_ANY);
|
||||
serverAddress.sin_port = htons(port_number);
|
||||
|
||||
if(bind(socketDescriptor,(struct sockaddr *) &serverAddress,sizeof(serverAddress))<0){
|
||||
cerr << "Can not bind socket "<<endl;
|
||||
socketDescriptor=-1;
|
||||
} else {
|
||||
listen(socketDescriptor, 5);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
MySocketTCP::MySocketTCP(const char* const host_ip_or_name, unsigned short int const port_number):
|
||||
last_keep_connection_open_action_was_a_send(0), file_des(-1), send_rec_max_size(SEND_REC_MAX_SIZE), is_a_server(0), portno(DEFAULT_PORTNO), socketDescriptor(-1)
|
||||
{ // sender (client): where to? ip
|
||||
//is_a_server = 0;
|
||||
// SetupParameters();
|
||||
strcpy(hostname,host_ip_or_name);
|
||||
portno=port_number;
|
||||
struct hostent *hostInfo = gethostbyname(host_ip_or_name);
|
||||
if (hostInfo == NULL){
|
||||
cerr << "Exiting: Problem interpreting host: " << host_ip_or_name << "\n";
|
||||
} else {
|
||||
// Set some fields in the serverAddress structure.
|
||||
serverAddress.sin_family = hostInfo->h_addrtype;
|
||||
memcpy((char *) &serverAddress.sin_addr.s_addr,
|
||||
hostInfo->h_addr_list[0], hostInfo->h_length);
|
||||
serverAddress.sin_port = htons(port_number);
|
||||
socketDescriptor=0; //You can use send and recv, //would it work?????
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
int MySocketTCP::getHostname(char *name) {
|
||||
if (is_a_server==0) {
|
||||
strcpy(name,hostname);
|
||||
}
|
||||
return is_a_server;
|
||||
};
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
int MySocketTCP::Connect(){
|
||||
|
||||
if(file_des>0) return file_des;
|
||||
|
||||
|
||||
if(is_a_server){ //server; the server will wait for the clients connection
|
||||
|
||||
|
||||
if (socketDescriptor>0) {
|
||||
if ((file_des = accept(socketDescriptor,(struct sockaddr *) &clientAddress, &clientAddress_length)) < 0) {
|
||||
|
||||
cerr << "Error: with server accept, connection refused"<<endl;
|
||||
|
||||
socketDescriptor=-1;
|
||||
}
|
||||
}
|
||||
file_des = socketDescriptor;
|
||||
} else {
|
||||
socketDescriptor = socket(AF_INET, SOCK_STREAM,0); //tcp
|
||||
|
||||
if (socketDescriptor < 0){
|
||||
cerr << "Can not create socket "<<endl;
|
||||
file_des = socketDescriptor;
|
||||
} else {
|
||||
|
||||
if(connect(socketDescriptor,(struct sockaddr *) &serverAddress,sizeof(serverAddress))<0){
|
||||
cerr << "Can not connect to socket "<<endl;
|
||||
file_des = -1;
|
||||
} else
|
||||
file_des = socketDescriptor;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return file_des;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
void MySocketTCP::Disconnect(){
|
||||
|
||||
if(file_des>=0){ //then was open
|
||||
if(is_a_server){
|
||||
close(file_des);
|
||||
}
|
||||
else {
|
||||
close(socketDescriptor);
|
||||
socketDescriptor=-1;
|
||||
}
|
||||
file_des=-1;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
int MySocketTCP::SendDataOnly(void* buf,int length){//length in characters
|
||||
|
||||
|
||||
#ifdef VERY_VERBOSE
|
||||
|
||||
cout << "want to send "<< length << " Bytes" << endl;
|
||||
#endif
|
||||
|
||||
if (file_des<0) return -1;
|
||||
int total_sent=0;
|
||||
while(length>0){
|
||||
int nsending = (length>send_rec_max_size) ? send_rec_max_size:length;
|
||||
int nsent = write(file_des,(char*)buf+total_sent,nsending);
|
||||
if(!nsent) break;
|
||||
length-=nsent;
|
||||
total_sent+=nsent;
|
||||
// cout<<"nsent: "<<nsent<<endl;
|
||||
}
|
||||
|
||||
#ifdef VERY_VERBOSE
|
||||
cout << "sent "<< total_sent << " Bytes" << endl;
|
||||
#endif
|
||||
return total_sent;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
int MySocketTCP::SendData(void* buf,int length){//length in characters
|
||||
int ndata = SendDataAndKeepConnection(buf,length);
|
||||
Disconnect();
|
||||
return ndata;
|
||||
}
|
||||
|
||||
int MySocketTCP::SendDataAndKeepConnection(void* buf,int length){//length in characters
|
||||
if(last_keep_connection_open_action_was_a_send) Disconnect(); //to keep a structured data flow;
|
||||
|
||||
Connect();
|
||||
int total_sent=SendDataOnly(buf,length);
|
||||
last_keep_connection_open_action_was_a_send=1;
|
||||
return total_sent;
|
||||
}
|
||||
|
||||
|
||||
int MySocketTCP::ReceiveDataOnly(void* buf,int length){//length in characters
|
||||
int total_received=0;
|
||||
if (file_des<0) return -1;
|
||||
#ifdef VERY_VERBOSE
|
||||
cout << "want to receive "<< length << " Bytes" << endl;
|
||||
#endif
|
||||
|
||||
while(length>0){
|
||||
int nreceiving = (length>send_rec_max_size) ? send_rec_max_size:length;
|
||||
int nreceived = read(file_des,(char*)buf+total_received,nreceiving);
|
||||
if(!nreceived) break;
|
||||
length-=nreceived;
|
||||
total_received+=nreceived;
|
||||
// cout<<"nrec: "<<nreceived<<" waiting for ("<<length<<")"<<endl;
|
||||
}
|
||||
|
||||
#ifdef VERY_VERBOSE
|
||||
cout << "received "<< total_received << " Bytes" << endl;
|
||||
|
||||
#endif
|
||||
|
||||
return total_received;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
int MySocketTCP::ReceiveData(void* buf,int length){//length in characters
|
||||
int ndata = ReceiveDataAndKeepConnection(buf,length);
|
||||
Disconnect();
|
||||
return ndata;
|
||||
}
|
||||
|
||||
int MySocketTCP::ReceiveDataAndKeepConnection(void* buf,int length){//length in characters
|
||||
if(!last_keep_connection_open_action_was_a_send) Disconnect(); //to a keep structured data flow;
|
||||
|
||||
Connect();
|
||||
// should preform two reads one to receive incomming char count
|
||||
int total_received=ReceiveDataOnly(buf,length);
|
||||
last_keep_connection_open_action_was_a_send=0;
|
||||
return total_received;
|
||||
}
|
||||
|
90
slsDetectorSoftware/MySocketTCP/MySocketTCP.h
Normal file
90
slsDetectorSoftware/MySocketTCP/MySocketTCP.h
Normal file
@ -0,0 +1,90 @@
|
||||
//version 1.0, base development ij 19/01/09
|
||||
|
||||
/* Modified by anna on 19.01.2008 */
|
||||
/*
|
||||
canceled SetupParameters() and varaibles intialized in the constructors' headers;
|
||||
defined SEND_REC_MAX_SIZE (for compatibilty with mythen (and possibly other) pure C servers (i would move it to the common header file)
|
||||
|
||||
added #ifndef C_ONLY... to cutout class definition when including in pure C servers (can be removed if SEND_REC_MAX_SIZE is moved to the common header file)
|
||||
|
||||
defined private variables char hostname[1000] and int portno to store connection informations;
|
||||
|
||||
defined public functions int getHostname(char *name) and int getPortNumber() to retrieve connection informations
|
||||
|
||||
added public function int getErrorStatus() returning 1 if socketDescriptor<0
|
||||
|
||||
remove exits in the constructors and replace them with socketDescriptor=-1
|
||||
|
||||
replaced the argument of send/receive data with void (to avoid too much casting or compiler errors/warnings)
|
||||
|
||||
added a function which really does not close the socket between send/receive (senddataonly, receivedataonly)
|
||||
|
||||
*/
|
||||
#ifndef MY_SOCKET_TCP_H
|
||||
#define MY_SOCKET_TCP_H
|
||||
|
||||
#define SEND_REC_MAX_SIZE 4096
|
||||
#define DEFAULT_PORTNO 1952
|
||||
|
||||
|
||||
#include <arpa/inet.h>
|
||||
#include <netdb.h>
|
||||
#include <netinet/in.h>
|
||||
#include <unistd.h>
|
||||
|
||||
|
||||
#include <math.h>
|
||||
|
||||
class MySocketTCP{
|
||||
|
||||
public:
|
||||
MySocketTCP(const char* const host_ip_or_name, unsigned short int const port_number); // sender (client): where to? ip
|
||||
MySocketTCP(unsigned short int const port_number); // receiver (server) local no need for ip
|
||||
~MySocketTCP();
|
||||
|
||||
int getHostname(char *name);
|
||||
int getPortNumber(){return portno;};
|
||||
int getErrorStatus(){if (socketDescriptor<0) return 1; else return 0;};
|
||||
|
||||
|
||||
|
||||
int Connect(); //establish connection a Disconnect should always follow
|
||||
void Disconnect(); //free connection
|
||||
|
||||
//The following two functions will connectioned->send/receive->disconnect
|
||||
int SendData(void* buf,int length);//length in characters
|
||||
int ReceiveData(void* buf,int length);
|
||||
|
||||
|
||||
//The following two functions stay connected, blocking other connections, and must be manually disconnected,
|
||||
// when the last call is a SendData() or ReceiveData() the disconnection will be done automatically
|
||||
//These function will also automatically disconnect->reconnect if
|
||||
// two reads (or two writes) are called in a row to preserve the data send/receive structure
|
||||
int SendDataAndKeepConnection(void* buf,int length);
|
||||
int ReceiveDataAndKeepConnection(void* buf,int length);
|
||||
|
||||
|
||||
// Danger! These functions do not connect nor disconnect nor flush the data! be sure that send-receive match perfectly on both server and client side!
|
||||
int SendDataOnly(void* buf,int length);
|
||||
int ReceiveDataOnly(void* buf,int length);
|
||||
|
||||
private:
|
||||
|
||||
char hostname[1000];
|
||||
int portno;
|
||||
|
||||
int is_a_server;
|
||||
int socketDescriptor;
|
||||
struct sockaddr_in clientAddress, serverAddress;
|
||||
socklen_t clientAddress_length;
|
||||
|
||||
int file_des;
|
||||
|
||||
int send_rec_max_size;
|
||||
bool last_keep_connection_open_action_was_a_send;
|
||||
|
||||
|
||||
// void SetupParameters();
|
||||
|
||||
};
|
||||
#endif
|
BIN
slsDetectorSoftware/MySocketTCP/rec
Executable file
BIN
slsDetectorSoftware/MySocketTCP/rec
Executable file
Binary file not shown.
31
slsDetectorSoftware/MySocketTCP/rec.cxx
Normal file
31
slsDetectorSoftware/MySocketTCP/rec.cxx
Normal file
@ -0,0 +1,31 @@
|
||||
|
||||
//version 1.0, base development ij 19/01/09
|
||||
|
||||
#include <iostream>
|
||||
#include "MySocketTCP.h"
|
||||
|
||||
using namespace std;
|
||||
|
||||
int main(){
|
||||
|
||||
char data[50000];
|
||||
int length=50000;
|
||||
|
||||
unsigned short int portnum = 1952;
|
||||
MySocketTCP* sock = new MySocketTCP(portnum);
|
||||
|
||||
cout<<"\tReceived :"<<sock->ReceiveDataAndKeepConnection(data,23000)<<endl;
|
||||
cout<<"\tReceived :"<<sock->ReceiveData(data,32200)<<endl;
|
||||
cout<<"\tReceived :"<<sock->ReceiveData(data,33300)<<endl;
|
||||
|
||||
|
||||
|
||||
cout<<"\tReceived :"<<sock->ReceiveData(data,30000)<<endl;
|
||||
|
||||
|
||||
cout<<"\tReceived :"<<sock->ReceiveData(data,3222)<<endl;
|
||||
|
||||
delete sock;
|
||||
|
||||
return 0;
|
||||
}
|
BIN
slsDetectorSoftware/MySocketTCP/send
Executable file
BIN
slsDetectorSoftware/MySocketTCP/send
Executable file
Binary file not shown.
40
slsDetectorSoftware/MySocketTCP/send.cxx
Normal file
40
slsDetectorSoftware/MySocketTCP/send.cxx
Normal file
@ -0,0 +1,40 @@
|
||||
|
||||
//version 1.0, base development ij 19/01/09
|
||||
|
||||
#include <iostream>
|
||||
#include "MySocketTCP.h"
|
||||
|
||||
using namespace std;
|
||||
|
||||
int main(int argc, char *argv[]){
|
||||
|
||||
if(argc!=2){
|
||||
cout<<"Usage: send ip_addess/hostName"<<endl;
|
||||
return 1;
|
||||
}
|
||||
cout<<"rec function must be first called."<<endl;
|
||||
|
||||
char ip_address[200];
|
||||
sprintf(ip_address,"%s",argv[1]);
|
||||
unsigned short int portnum = 1952;
|
||||
|
||||
|
||||
char data[50000];
|
||||
int length=50000;
|
||||
|
||||
MySocketTCP* sock = new MySocketTCP(ip_address,portnum);
|
||||
|
||||
cout<<"\tSending :"<<sock->SendDataAndKeepConnection(data,2000)<<endl;
|
||||
cout<<"\tSending :"<<sock->SendData(data,2200)<<endl;
|
||||
cout<<"\tSending :"<<sock->SendData(data,1200)<<endl;
|
||||
|
||||
|
||||
cout<<"\tSending :"<<sock->SendData(data,25000)<<endl;
|
||||
|
||||
cout<<"\tSending :"<<sock->SendData(data,222)<<endl;
|
||||
|
||||
delete sock;
|
||||
|
||||
|
||||
return 0;
|
||||
}
|
Reference in New Issue
Block a user