En cours

Convert C code to C#

I am looking to have class converted from C to C#. See attached file with the C code that needs to be converted to C#.

C Code:

/****** huge number of includes for fancy network processing ********/
#include
#include
#include
#include
#include
#include
#include
#include

// copied from the demo app, but without the CRC etc, because
// SerialForwarder hides these fields from us
typedef struct TOS_Msg
{
/* The following fields are transmitted/received by serial forwarder. */
uint8_t length;
uint8_t fcfhi;
uint8_t fcflo;
uint8_t dsn;
uint16_t destpan;
uint16_t addr;
uint8_t type;
uint8_t group;
int8_t data[28]; // max 28 bytes, but we'll use much less
} TOS_Msg;
typedef TOS_Msg * TOS_MsgPtr;

// copied from the demo app
typedef struct DemoMsg {
uint16_t source;
uint16_t number;
char color;
} DemoMsg;

typedef DemoMsg * DemoMsgPtr;

/**** Read Input Line **************/
// this is some old code I copied from another program to
// read a line of input from the keyboard
int getline( char s[], int lim ) {
int c, i;
for (i=0; ilength + sizeof(TOS_Msg) - 28;
s = (char *) p;
// SerialForwarder expects the first byte to be a count
// of the number of bytes in the TOS_Msg.
r = write(socket, &n, 1);
// printf("sent %xn",n);
if (r < 0) { perror("putStream error"); exit(errno); }

for (i = 0; i < n; i++) {
r = write(socket, &s[i], 1);
// printf("sent %xn",*(s+i));
if (r < 0) { perror("putStream error"); exit(errno); }
}
}

/**** Read Stream from Socket ******/
void getStream( int socket, TOS_MsgPtr p ) {
int i, r;
char * s;
uint8_t n;
extern int errno;
s = (char *) p;

// first byte read should be the count of the number
// of bytes in the received TOS_Msg
r = read(socket, &n, 1);
if (r sizeof(TOS_Msg) ) {
printf("wierd message for %d bytes returnedn",n);
exit(1);
}
for (i = 0; i < n; i++) {
r = read(socket, &s[i], 1);
if (r < 0) { perror("getStream error"); exit(errno); }
if (r == 0) { printf("connection broken!n"); exit(0); }
}
return;
}


/**** Exchange SF Protocol Number ******/
// wierd protocol code needed by SerialForwarder -- totally
// undocumented, I had to read the code of SFProtocol.java
void exchange( int socket ) {
int i, r;
char s[6];
extern int errno;
memset(s,0,sizeof(s));
for (i = 0; i < 2; i++) {
r = read(socket, &s[i], 1);
if (r < 0) { perror("getExchange error"); exit(errno); }
if (r == 0) { printf("connection broken!n"); exit(0); }
}
if ( !(s[0] == 'T' & s[1] == '!') ) {
printf("SFProtocol exchange failedn"); exit(0);
}
s[1] = '!';
for (i = 0; i < 6; i++) {
r = write(socket, &s[i], 1);
if (r h_addr, (char *) &toServer.sin_addr, h->h_length);

sock = socket(AF_INET, SOCK_STREAM, 0);
if (sock < 0) { perror("unable to create socket"); exit(errno); }
i = 1;
r = setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (char *) &i, sizeof (i));
if (r < 0) { perror("setsockopt error");
close(sock); exit(errno); }
r = connect(sock, (struct sockaddr *) &toServer, sizeof(toServer));
if (r < 0) { perror("connect error"); exit(errno); }

// use the "poll()" function so that we don't get stuck
// trying to read from a socket that has no data
// use "man poll" to read about this neat system function
waitor.fd = sock;
waitor.events = POLLIN;

// do the silly "version exchange" that SFProtocol.java needs
exchange(sock);

while (1) {
printf("nEnter R, G, or Y: ");
getline(message,256);
if (strlen(message) source = 0;
dp->number = 255;
dp->color = message[0];
putStream(sock,&buffer);

/***** Loop to read any response(s) ********************/
for (;;) {

// wait for 250 milliseconds for a message to return
poll(&waitor,1,250);
if (!(waitor.revents & POLLIN)) break; // no message!
getStream(sock,&buffer);

printf("Returned from moten");
printf("t address %x",buffer.addr);
printf("t type %x",buffer.type);
printf("t group %x",buffer.group);
printf("t length %xn",buffer.length);
dp = (DemoMsgPtr) buffer.data;
printf("t source %x",dp->source);
printf("t number %x",dp->number);
printf("t color %cn",dp->color);
}
}
}

Compétences : .NET, Programmation C

Voir plus : convert code code, code converter, converting code, converts code, code convert, convert program, uint16_t convert, converter code, program convert dotnet, convert uint16_t uint8_t, convert char uint8_t, convert character int, convert class, code conversion, convert net, no code, code that, code 1, & code, code code, code uint16_t, convert uint8_t char, convert code source code forum, exite project code, convert sprintf

Concernant l'employeur :
( 2 commentaires ) Memphis, United States

N° du projet : #53669