You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
451 lines
10 KiB
451 lines
10 KiB
#include "serverPort.h"
|
|
|
|
#define LOG( X ) m_pLog->Log( "serverPort", this, X )
|
|
#define LOG_ERROR( X ) m_pLog->Log( "serverPort", this, _LOG_ERROR, X )
|
|
|
|
//#define DEBUG_EXTRA
|
|
//#define DUBUG_COMMUNICATION
|
|
|
|
#ifdef __linux__
|
|
serverPort::serverPort( int AcceptedSocket ):transportSocket(){
|
|
#else
|
|
serverPort::serverPort( SOCKET AcceptedSocket ):transportSocket(){
|
|
#endif
|
|
m_pLog = basicLogCollection::getInstance();
|
|
m_nSocketFD = AcceptedSocket;
|
|
m_bKeepAlive = true;
|
|
};
|
|
|
|
|
|
serverPort::~serverPort(){
|
|
};
|
|
|
|
int serverPort::sendBlock(unsigned char *Data, int DataSize){
|
|
if( !m_bInitComplete || m_eConnectionStatus!= CS_CONNECTED ) return -1; // Exit after unsuccess reconnect
|
|
|
|
#ifdef __linux__
|
|
int RetVal;
|
|
if( m_nSocketFD > -1 ){
|
|
#ifdef DUBUG_COMMUNICATION
|
|
char cBuffer[256];
|
|
sprintf(cBuffer,"Write into Socket %d bytes", DataSize );
|
|
LOG(cBuffer);
|
|
#endif
|
|
RetVal = write(m_nSocketFD, Data, DataSize);
|
|
//LOG("Data writen into Socket");
|
|
if( ( RetVal == -1 ) && (errno != EINTR ) && ( errno != EWOULDBLOCK ) ) {
|
|
LOG_ERROR("Error acures while writing data into the Socket");
|
|
shutdown( m_nSocketFD, 2 );
|
|
close( m_nSocketFD );
|
|
m_nSocketFD = -1;
|
|
//m_bConnected = false;
|
|
setConnectionStatus( CS_DISCONNECTED );
|
|
return -1;
|
|
};
|
|
if( RetVal < 0 ) RetVal = 0;
|
|
#ifdef DUBUG_COMMUNICATION
|
|
sprintf(cBuffer,"Bytes Sent: %ld", RetVal);
|
|
LOG(cBuffer);
|
|
#ifdef DUBUG_COMMUNICATION
|
|
for (int i = 0; i < RetVal; i++) {
|
|
sprintf(cBuffer, "0x%02X", Data[i]);
|
|
LOG(cBuffer);
|
|
};
|
|
#endif
|
|
#endif
|
|
} else {
|
|
LOG_ERROR("Not Connected!");
|
|
//m_bConnected = false;
|
|
setConnectionStatus( CS_DISCONNECTED );
|
|
return -1;
|
|
};
|
|
#else
|
|
DWORD RetVal;
|
|
if (m_nSocketFD != INVALID_SOCKET){
|
|
char cBuffer[256];
|
|
// Send an initial buffer
|
|
RetVal = send(m_nSocketFD, (const char *)Data, DataSize, NULL);
|
|
if (RetVal == SOCKET_ERROR) {
|
|
sprintf(cBuffer,"send failed with error: %d", WSAGetLastError());
|
|
LOG_ERROR(cBuffer);
|
|
closesocket(m_nSocketFD);
|
|
WSACleanup();
|
|
//m_bConnected = false;
|
|
setConnectionStatus( CS_DISCONNECTED );
|
|
return -1;
|
|
};
|
|
#ifdef DUBUG_COMMUNICATION
|
|
sprintf(cBuffer,"Bytes Sent: %ld", RetVal);
|
|
#endif
|
|
LOG(cBuffer);
|
|
if (!m_bKeepAlive){
|
|
closesocket(m_nSocketFD);
|
|
WSACleanup();
|
|
//m_bConnected = false;
|
|
m_nSocketFD = INVALID_SOCKET;
|
|
setConnectionStatus( CS_DISCONNECTED );
|
|
};
|
|
} else {
|
|
//m_bConnected = false;
|
|
setConnectionStatus( CS_DISCONNECTED );
|
|
return -1;
|
|
};
|
|
#endif
|
|
return RetVal;
|
|
};
|
|
|
|
int serverPort::recptBlock(unsigned char *Buffer, int *BufferLength){
|
|
//char cBuffer[256];// Buffer For Messages Only
|
|
if( !m_bInitComplete || m_eConnectionStatus!= CS_CONNECTED ){
|
|
LOG_ERROR("Not Connected!");
|
|
m_bInitComplete = false;
|
|
//m_bConnected = false;
|
|
setConnectionStatus( CS_DISCONNECTED );
|
|
return -1; // Exit after unsuccess reconnect
|
|
}
|
|
|
|
#ifdef __linux__
|
|
int RetVal;
|
|
if( m_nSocketFD > -1 ) {
|
|
//char cBuffer[256];
|
|
//sprintf(cBuffer,"Read from Socket %p", Buffer );
|
|
//LOG(cBuffer);
|
|
RetVal = recv( m_nSocketFD, Buffer, MAXIMAL_SOCKET_PACKET_LENGTH, 0 );//
|
|
//RetVal = read(m_nSocketFD, Buffer, MAXIMAL_SOCKET_PACKET_LENGTH);
|
|
if( RetVal < 1 ){
|
|
if( errno!=EAGAIN && errno!=EINTR ) {
|
|
LOG_ERROR("Peer Closes Connection!");
|
|
shutdown( m_nSocketFD, 2 );
|
|
close( m_nSocketFD );
|
|
*BufferLength = 0;
|
|
//m_bConnected = false;
|
|
m_nSocketFD = -1;
|
|
setConnectionStatus( CS_DISCONNECTED );
|
|
return -1;
|
|
};
|
|
};
|
|
//if (!m_bKeepAlive ){
|
|
// //LOG("KeepAlive Disabled - Clozing Socket");
|
|
// shutdown( m_nSocketFD, SHUT_RDWR ); //SD_RECEIVE
|
|
// close( m_nSocketFD );
|
|
// m_bStatus = 0;
|
|
// m_nSocketFD = -1;
|
|
//};
|
|
} else {
|
|
LOG_ERROR("Not Connected!");
|
|
*BufferLength = 0;
|
|
//m_bConnected = false;
|
|
setConnectionStatus( CS_DISCONNECTED );
|
|
return -1;
|
|
};
|
|
#else
|
|
DWORD RetVal;
|
|
if( m_nSocketFD != INVALID_SOCKET ){
|
|
// Receive until the peer closes the connection
|
|
RetVal = recv(m_nSocketFD, (char *)Buffer, MAXIMAL_SOCKET_PACKET_LENGTH, 0);
|
|
if( RetVal < 1 ){
|
|
if( errno!=EAGAIN && errno!=EINTR ) {
|
|
closesocket(m_nSocketFD);
|
|
WSACleanup();
|
|
*BufferLength = 0;
|
|
//m_bConnected = false;
|
|
m_nSocketFD = INVALID_SOCKET;
|
|
setConnectionStatus( CS_DISCONNECTED );
|
|
return -1;
|
|
};
|
|
};
|
|
//if (!m_bKeepAlive){
|
|
// closesocket(m_nSocketFD);
|
|
// WSACleanup();
|
|
// m_bConnected = false;
|
|
// m_nSocketFD = INVALID_SOCKET;
|
|
//};
|
|
} else {
|
|
LOG_ERROR("Not Connected!");
|
|
*BufferLength = 0;
|
|
//m_bConnected = false;
|
|
setConnectionStatus( CS_DISCONNECTED );
|
|
return -1;
|
|
};
|
|
#endif // linux
|
|
if (RetVal > 0) {
|
|
#ifdef DEBUG_EXTRA
|
|
char cBuffer[165];
|
|
sprintf(cBuffer, "Bytes received: %d", RetVal);
|
|
LOG(cBuffer);
|
|
#ifdef DUBUG_COMMUNICATION
|
|
for (int i = 0; i < RetVal; i++) {
|
|
sprintf(cBuffer, "0x%02X", Buffer[i]);
|
|
LOG(cBuffer);
|
|
};
|
|
#endif
|
|
#endif
|
|
*BufferLength = RetVal;
|
|
return RetVal;
|
|
};
|
|
|
|
*BufferLength = 0;
|
|
|
|
if (RetVal == -1) return 0;
|
|
|
|
if (RetVal == 0){
|
|
errno = 0;
|
|
return 0;
|
|
};
|
|
|
|
/* r < -1, should never happen. */
|
|
errno = EIO;
|
|
|
|
return 0;
|
|
};
|
|
|
|
|
|
//#undef DEBUG_EXTRA
|
|
//#undef DUBUG_COMMUNICATION
|
|
#undef LOG
|
|
#undef LOG_ERROR
|
|
/*
|
|
|
|
|
|
#include<stdio.h>
|
|
#include<sys/socket.h>
|
|
#include<sys/types.h>
|
|
#include<netinet/in.h>
|
|
#include<netdb.h>
|
|
#include<errno.h>
|
|
#include<string.h>
|
|
#include<netinet/if_ether.h>
|
|
#include<net/if.h>
|
|
#include<linux/sockios.h>
|
|
int main()
|
|
{
|
|
int socket1, socket2;
|
|
socket1 = 0;
|
|
socket2 = 0;
|
|
struct sockaddr_in server, client;
|
|
int returnstatus = 0;
|
|
socket1 = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
|
|
if(socket1 == -1)
|
|
{
|
|
perror("socket()");
|
|
}
|
|
|
|
server.sin_family = AF_INET;
|
|
server.sin_addr.s_addr = INADDR_ANY;
|
|
server.sin_port = htons(8888);
|
|
returnstatus = bind(socket1, (struct sockaddr*)&server, sizeof(server));
|
|
if(returnstatus == -1)
|
|
{
|
|
perror("bind()");
|
|
}
|
|
returnstatus = listen(socket1, 5);
|
|
if(returnstatus == -1)
|
|
{
|
|
perror("listen()");
|
|
}
|
|
for(;;)
|
|
{
|
|
int buffr[4];
|
|
int addrlen, n;
|
|
addrlen = sizeof(client);
|
|
socket2 = accept(socket1, (struct sockaddr *)&client, &addrlen);
|
|
printf("%s\n",inet_ntoa(client.sin_addr));
|
|
printf("%d\n",(int) ntohs(client.sin_port));
|
|
struct arpreq arpreq_;
|
|
bzero(&arpreq_, sizeof(struct arpreq));
|
|
|
|
if( ( n = ioctl(socket2, SIOCGARP, &arpreq_) ) < 0 ){
|
|
perror("ioctl");
|
|
}
|
|
|
|
unsigned char *ptr = &arpreq_.arp_ha.sa_data[0];
|
|
printf("MAC: %x:%x:%x:%x:%x:%x\n", *ptr, *(ptr+1), *(ptr+2), *(ptr+3),
|
|
*(ptr+4), *(ptr+5));
|
|
|
|
if (socket2 == -1)
|
|
{
|
|
perror("accept()");
|
|
}
|
|
returnstatus = read(socket2, buffr, sizeof(buffr));
|
|
|
|
if(returnstatus == -1)
|
|
{
|
|
perror("read()");
|
|
}
|
|
int c[2];
|
|
int *w;
|
|
c[0] = buffr[0] + buffr[1];
|
|
w = c;
|
|
returnstatus = write(socket2, w, sizeof(c));
|
|
if(returnstatus == -1)
|
|
{
|
|
perror("write()");
|
|
}
|
|
close(socket2);
|
|
|
|
}
|
|
close(socket1);
|
|
return 0;
|
|
}
|
|
|
|
int addition(int x, int y)
|
|
{
|
|
int z;
|
|
z = x + y;
|
|
return(z);
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Sorry to have left you for a while. I actually had trouble re-finding the code on the internet, after finding numerous instances on my first search. The version I had tried and tested was on a computer that was unavailable to me for some time. Anyway, here it is, and it works for me. There is a network device name hardcoded into the source code; edit as necessary
|
|
|
|
Code:
|
|
#include <errno.h>
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include <sys/socket.h>
|
|
#include <sys/ioctl.h>
|
|
#include <net/if_arp.h>
|
|
#include <netinet/in.h>
|
|
#include <linux/sockios.h>
|
|
|
|
static char *ethernet_mactoa(struct sockaddr *addr) {
|
|
static char buff[256];
|
|
|
|
unsigned char *ptr = (unsigned char *) addr->sa_data;
|
|
|
|
sprintf(buff, "%02X:%02X:%02X:%02X:%02X:%02X",
|
|
(ptr[0] & 0xff), (ptr[1] & 0xff), (ptr[2] & 0xff),
|
|
(ptr[3] & 0xff), (ptr[4] & 0xff), (ptr[5] & 0xff));
|
|
|
|
return (buff);
|
|
|
|
}
|
|
|
|
|
|
int main(int argc, char *argv[]) {
|
|
int s;
|
|
struct arpreq areq;
|
|
struct sockaddr_in *sin;
|
|
struct in_addr ipaddr;
|
|
|
|
if (argc != 2) {
|
|
fprintf(stderr, "-- Usage: %s ipaddress\n", argv[0]);
|
|
exit(1);
|
|
}
|
|
|
|
// Get an internet domain socket.
|
|
if ((s = socket(AF_INET, SOCK_DGRAM, 0)) == -1) {
|
|
perror("socket");
|
|
exit(1);
|
|
}
|
|
|
|
// Make the ARP request.
|
|
memset(&areq, 0, sizeof(areq));
|
|
sin = (struct sockaddr_in *) &areq.arp_pa;
|
|
sin->sin_family = AF_INET;
|
|
|
|
if (inet_aton(argv[1], &ipaddr) == 0) {
|
|
fprintf(stderr, "-- Error: bad dotted-decimal IP '%s'.\n",
|
|
argv[1]);
|
|
exit(1);
|
|
}
|
|
|
|
sin->sin_addr = ipaddr;
|
|
sin = (struct sockaddr_in *) &areq.arp_ha;
|
|
sin->sin_family = ARPHRD_ETHER;
|
|
|
|
strncpy(areq.arp_dev, "eth1", 15);
|
|
|
|
if (ioctl(s, SIOCGARP, (caddr_t) &areq) == -1) {
|
|
perror("-- Error: unable to make ARP request, error");
|
|
exit(1);
|
|
}
|
|
printf("%s (%s) -> %s\n", argv[1],
|
|
inet_ntoa(&((struct sockaddr_in *) &areq.arp_pa)->sin_addr),
|
|
ethernet_mactoa(&areq.arp_ha));
|
|
return 0;
|
|
}
|
|
|
|
|
|
#include<stdio.h>
|
|
#include<sys/socket.h>
|
|
#include<sys/types.h>
|
|
#include<netinet/in.h>
|
|
#include<netdb.h>
|
|
#include<errno.h>
|
|
#include<string.h>
|
|
int main()
|
|
{
|
|
int socket1, socket2;
|
|
socket1 = 0;
|
|
socket2 = 0;
|
|
struct sockaddr_in server, client;
|
|
int returnstatus = 0;
|
|
socket1 = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
|
|
if(socket1 == -1)
|
|
{
|
|
perror("socket()");
|
|
}
|
|
|
|
server.sin_family = AF_INET;
|
|
server.sin_addr.s_addr = INADDR_ANY;
|
|
server.sin_port = htons(8888);
|
|
returnstatus = bind(socket1, (struct sockaddr*)&server, sizeof(server));
|
|
if(returnstatus == -1)
|
|
{
|
|
perror("bind()");
|
|
}
|
|
returnstatus = listen(socket1, 5);
|
|
if(returnstatus == -1)
|
|
{
|
|
perror("listen()");
|
|
}
|
|
for(;
|
|
{
|
|
int buffr[4];
|
|
int addrlen;
|
|
addrlen = sizeof(client);
|
|
socket2 = accept(socket1, (struct sockaddr *)&client, &addrlen);
|
|
printf("%s\n",inet_ntoa(client.sin_addr));
|
|
printf("%d\n",(int) ntohs(client.sin_port));
|
|
if (socket2 == -1)
|
|
{
|
|
perror("accept()");
|
|
}
|
|
returnstatus = read(socket2, buffr, sizeof(buffr));
|
|
|
|
if(returnstatus == -1)
|
|
{
|
|
perror("read()");
|
|
}
|
|
int c[2];
|
|
int *w;
|
|
c[0] = buffr[0] + buffr[1];
|
|
w = c;
|
|
returnstatus = write(socket2, w, sizeof(c));
|
|
if(returnstatus == -1)
|
|
{
|
|
perror("write()");
|
|
}
|
|
close(socket2);
|
|
|
|
}
|
|
close(socket1);
|
|
return 0;
|
|
}
|
|
|
|
int addition(int x, int y)
|
|
{
|
|
int z;
|
|
z = x + y;
|
|
return(z);
|
|
}
|
|
|
|
|
|
|
|
*/
|
|
|