Quantcast
Channel: Intel® Software - Intel® C++ Compiler
Viewing all articles
Browse latest Browse all 1175

intel C++ in windows / linux on a socket.c file...

$
0
0

Hi all

in Linux, with ICC (16.0.3 on IA-32) I can compile a socket.c file and hence I can make a .a file 
which contains the following C program (see below for <socket.c>)

But in WINDOWS [7] version of ICL (16.0.3) I couldnt successfully compile it....

(I tried added such as ...

#ifdef WIN32
   #include "winsock.h"
   #include "winsock2.h"
   #include <windows.h>
#else
   #include <sys/socket.h>
   #include <sys/un.h>
#endif

  etc. but no use of it ....)

Here I need an advice - how I can I compile this file in Windows using ICL (intels C) compiler ?

I really need this file to make a porting job  - which port a linux application into Windows. Please also note that
The same file is compiled in GCC of Cygwin in windows without any trouble !

Haopy some can help me

With best regards

Krishna mohan 

 

/* A minimal wrapper for socket communication.

Copyright (C) 2013, Joshua More and Michele Ceriotti
Copyright (C) 2016, Bálint Aradi (adapted to F2003 C-bindings)

...
Functions:
   error: Prints an error message and then exits.
   open_socket_: Opens a socket with the required host server, socket type and
      port number.
   write_buffer_: Writes a string to the socket.
   read_buffer_: Reads data from the socket.
*/

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <sys/un.h>
#include <netdb.h>

void connect_inet_socket(int *psockfd, const char* host, int port)
/* Opens an internet socket.

   Note that fortran passes an extra argument for the string length,
   but this is ignored here for C compatibility.

   Args:
   psockfd: The id of the socket that will be created.
   port: The port number for the socket to be created. Low numbers are
         often reserved for important channels, so use of numbers of 4
         or more digits is recommended.
   host: The name of the host server.
*/

{
  int sockfd, ai_err;

  // creates an internet socket

  // fetches information on the host
  struct addrinfo hints, *res;
  char service[256];

  memset(&hints, 0, sizeof(hints));
  hints.ai_socktype = SOCK_STREAM;
  hints.ai_family = AF_UNSPEC;
  hints.ai_flags = AI_PASSIVE;

  sprintf(service, "%d", port); // convert the port number to a string
  ai_err = getaddrinfo(host, service, &hints, &res);
  if (ai_err!=0) {
    printf("Error code: %i\n",ai_err);
    perror("Error fetching host data. Wrong host name?");
    exit(-1);
  }

  // creates socket
  sockfd = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
  if (sockfd < 0) {
    perror("Error opening socket");
    exit(-1);
  }

  // makes connection
  if (connect(sockfd, res->ai_addr, res->ai_addrlen) < 0) {
    perror("Error opening INET socket: wrong port or server unreachable");
    exit(-1);
  }
  freeaddrinfo(res);

  *psockfd = sockfd;
}

void connect_unix_socket(int *psockfd, const char* pathname)
/* Opens a unix socket.

   Note that fortran passes an extra argument for the string length,
   but this is ignored here for C compatibility.

   Args:
   psockfd: The id of the socket that will be created.
   pathname: The name of the file to use for sun_path.
*/

{
  int sockfd, ai_err;

  struct sockaddr_un serv_addr;

  printf("Connecting to :%s:\n",pathname);

  // fills up details of the socket addres
  memset(&serv_addr, 0, sizeof(serv_addr));
  serv_addr.sun_family = AF_UNIX;
  /* Beware of buffer over runs
     UNIX Network Programming by Richard Stevens mentions
     that the use of sizeof() is ok, but see
http://mail-index.netbsd.org/tech-net/2006/10/11/0008.html

  */
  if ((int)strlen(pathname)> sizeof(serv_addr.sun_path)) {
    perror("Error opening UNIX socket: pathname too long\n");
    exit(-1);
  } else {
    strcpy(serv_addr.sun_path, pathname);
  }
  // creates a unix socket

  // creates the socket
  sockfd = socket(AF_UNIX, SOCK_STREAM, 0);

  // connects
  if (connect(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0) {
    perror("Error opening UNIX socket: path unavailable, or already existing");
    exit(-1);
  }
  *psockfd = sockfd;
}

void writebuffer_socket(int sockfd, const void *data, int len)
/* Writes to a socket.

Args:
   sockfd: The id of the socket that will be written to.
   data: The data to be written to the socket.
   len: The length of the data in bytes.
*/
{
   int n;

   n = write(sockfd, (char *) data, len);
   if (n < 0) {
     perror("Error writing to socket: server has quit or connection broke");
     exit(-1);
   }
}


void readbuffer_socket(int sockfd, void *data, int len)
/* Reads from a socket.

Args:
   sockfd: The id of the socket that will be read from.
   data: The storage array for data read from the socket.
   len: The length of the data in bytes.
*/

{
   int n, nr;
   char *pdata;

   pdata = (char *) data;
   n = nr = read(sockfd, pdata, len);

   while (nr > 0 && n < len) {
     nr = read(sockfd, &(pdata[n]), len - n);
     n += nr;
   }
   if (n == 0) {
     perror("Error reading from socket: server has quit or connection broke");
     exit(-1);
   }
}


void shutdown_socket(int sockfd)
/* Shuts down the socket.
*/
{
  shutdown(sockfd, 2);
  close(sockfd);
}

 

Zone: 


Viewing all articles
Browse latest Browse all 1175

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>