tags:

views:

50

answers:

1

Hi Experts,

I'm not a software person, but I could really use some advice.

I'm writing a C program (cut/paste below) to establish a TCP socket connection from my Mac Pro to a Windows XP-based test-instrument sitting next to it over LAN (Ethernet). The program compiles without any warnings or errors. But executing the code using GNU Debugger, I can see it exits at 'exit(2)' which is the "if(connect(MySocket" line. There's no timeout, it just exits immediately.

I compile using: gcc -g -Wall talk2me.c -o talk2me but I don't get any hints in the output nor when debugging at to what might be the issue.

I'm sure the 10.0.1.100 and port 5025 are correct (using Matlab code I can communicate fine using these parameters). Any idea where else I could look to debug?

Outside of the code itself, are there any other requirements (perhaps system-level) that need to be satisfied (like running the code from a certain directory, or setting a parameter in unix to allow connections, etc.)? It may be something obvious that I'm missing because I'm a hardware guy, so feel free to assume I'm doing something stupid. I can run a 'hello world' program fine, it that helps. Thanks in advance, ggk

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <netinet/tcp.h>
#include <netinet/in.h>
#include <arpa/inet.h>   //for inet_addr
#include <unistd.h>   // for function 'close'

int main(void)
{
int MySocket;
if((MySocket=socket(PF_INET,SOCK_STREAM,0))==-1) exit(1);

struct in_addr {
  unsigned long s_addr;
};
struct sockaddr_in {
 short int sin_family;    // Address family
 unsigned short int sin_port;  // Port number
 struct in_addr sin_addr;   // Internet address
 unsigned char sin_zero[8];   // Padding
};
struct sockaddr_in MyAddress;

// Initialize the whole structure to zero
memset(&MyAddress,0,sizeof(struct sockaddr_in));
// Then set the individual fields
MyAddress.sin_family=PF_INET;   // IPv4
MyAddress.sin_port=htons(5025);  // Port number used by instrument
MyAddress.sin_addr.s_addr=inet_addr("10.0.1.100"); // IP Address

if(connect(MySocket,(struct sockaddr *) &MyAddress, 
     sizeof(struct sockaddr_in))==-1) exit(2);  

// Send SCPI command
if(send(MySocket,"*IDN?\n",6,0)==-1) exit(3);

// Read response
char buffer[200];
int actual;
if((actual=recv(MySocket,&buffer[0],200,0))==-1) exit(4);
buffer[actual]=0; // Add zero character (C string)
printf("Instrument ID: %s\n",buffer);

// Close socket
if(close(MySocket)==-1) exit(99);

return 0;
}
+7  A: 
  1. You have defined struct in_addr and struct sockaddr_in yourself at the top of main. Do not do that, these are types found in header files(netinet/in.h), you have to use those, not your own versions.

  2. Try connection with telnet from your mac box to 10.0.1.100 port 5025 , does that work ?

  3. replace that exit(2); with {perror("connect"); exit(2); } to get a description of what is wrong.

nos
Thanks so much!1. Could you send me the code with your recommended changes for those struct's?2. Good point. Yes, it seems to connect fine: gkk$ telnet 10.0.1.100 5025 Trying 10.0.1.100... Connected to 10.0.1.100. Escape character is '^]'.3. Done (THANKS!). Here's the result: (gdb) run Starting program: /Users/gkk/talk2me Reading symbols for shared libraries +. done connect(): Invalid argumentProgram exited with code 02.------Does this provide any clue what's going on?
ggkmath
For 1. I just commented out those struc's and now it's working. THANK YOU!!!!!!
ggkmath