Link to home
Start Free TrialLog in
Avatar of anuravi
anuravi

asked on

Socket, C, Thread

Scenario : -
We are architecting a solution utilizing Sockets ( TCP /IP ). Essentially a Client  will be connecting to a Server listnening on a pre-defined port.

This is my understanding.... If there are multiple clients connecting to the Java server (JS) ( which is in Java ), ther server can create a socket and spawn a thread process to service the request received.

Question :-- I discovered that my client is going to a single server (SS) ( written in "C") sending request to JS.

Where can I find a program/Link/details that will create, so to speak thread(s) and initiate a connection to JS.

Help would be greatly appreciated.
Points no under limit.
Avatar of nebeker
nebeker

So what you're saying (if I understood your question) is that your client talks to a server, which turns around and talks to another server -- and you want to cut out the middleman (i.e. the server written in C) and talk directly to the end server (the one written in Java), correct?

Well, you can easily connect to the Java-based server, but you will have to know what the message protocol is to talk to that particular server.  As for establishing the connection, the process will be EXACTLY the same as what you're doing now, except you'll use a different port number and potentially a different host name....
You may not be able to connect to the JS directly from your client machine, maybe only from the machine where the SS is running, you should test this first, or give some idea of the architecture (firewalls etc.) which you are involved with.
It would help for you to clarify a couple of things:

What do you mean by single server (SS)?  Is this the client?

Also, using beginthread() will create a thread.  You pass it a function defined as:

struct MYDATA
{
  int myint1;
  int more;
  ...
  ...
};

void MyThreadedFunction( void *data )
{
  MYDATA * p
  p = (MYDATA *)data;
  //Work goes here!

  ...

  //When done
  free( string );
  printf( "Memory freed\n" );
}

main()
{
  MYDATA * data;

  /* Allocate space for a path name */
  data = malloc( sizeof(MYDATA) );
  if( data == NULL )
     printf( "Insufficient memory available\n" );
  else
  {
    beginthread( MyThreadedFunction,0,(void *)data );
            or
    beginthread( MyThreadedFunction,0, NULL /*No data*/ );
  }
}

But the code above would be the beginnings of a multi-threaded server ... you listen, get a connection, spawn a thread to handle the connection.

The other thing that I know is important to point out is that you may run into a "Little Endian" and "Big Endian" problem between Java and C.

Hope this helps.

Brian
It would help for you to clarify a couple of things:

What do you mean by single server (SS)?  Is this the client?

Also, using beginthread() will create a thread.  You pass it a function defined as:

struct MYDATA
{
  int myint1;
  int more;
  ...
  ...
};

void MyThreadedFunction( void *data )
{
  MYDATA * p
  p = (MYDATA *)data;
  //Work goes here!

  ...

  //When done
  free( string );
  printf( "Memory freed\n" );
}

main()
{
  MYDATA * data;

  /* Allocate space for a path name */
  data = malloc( sizeof(MYDATA) );
  if( data == NULL )
     printf( "Insufficient memory available\n" );
  else
  {
    beginthread( MyThreadedFunction,0,(void *)data );
            or
    beginthread( MyThreadedFunction,0, NULL /*No data*/ );
  }
}

But the code above would be the beginnings of a multi-threaded server ... you listen, get a connection, spawn a thread to handle the connection.

The other thing that I know is important to point out is that you may run into a "Little Endian" and "Big Endian" problem between Java and C.

Hope this helps.

Brian
Sorry for the double posting ... I don't know how that happened
You can make socket connection as usual no matter what server you use (you said that you use Java Server). You can use MSDN as refference (for Microsoft Visual C++). Here is the example.

#include <conio.h>
#include <time.h>
#include <winsock2.h>

unsigned long destip=0;
unsigned short destport=0;
unsigned int reqnum=0,succnum=0;
DWORD *thdid;
HANDLE *thdhnd;
SOCKET *sd;
sockaddr_in addr;

DWORD WINAPI thdproc(LPVOID);

void main(unsigned int argc, char* argv[])
{
     unsigned int i=0;
     WSADATA wsaData;
     clock_t start=0,stop=0;
     if((argc<=3)||((reqnum=atoi(argv[3]))<=0)) {
          reqnum=1;
     }
     if((argc>=3)&&((destip=inet_addr(argv[1]))!=-1)) {
          thdid=new DWORD[reqnum];
          thdhnd=new HANDLE[reqnum];
          sd=new SOCKET[reqnum];
          destport=htons(atoi(argv[2]));
          cprintf("Creating %d socket(s) to %s:%s\n",reqnum,argv[1],argv[2]);
          WSAStartup(MAKEWORD(2,2),&wsaData);
          addr.sin_family=AF_INET;
          addr.sin_addr.S_un.S_addr=destip;
          addr.sin_port=destport;
          start=clock();
          for(i=0;i<reqnum;i++) {
               thdhnd[i]=CreateThread(NULL,0,thdproc,&sd[i],0,&(thdid[i]));
          }
          for(i=0;i<reqnum;i++) WaitForSingleObject(thdhnd[i],INFINITE);
          stop=clock();
          cprintf("Press anykey to close all request\n");
          getch();
          for(i=0;i<reqnum;i++) {
               if(thdid[i]!=NULL) {
                    if(sd[i]!=INVALID_SOCKET) {
                         closesocket(sd[i]);
                    }
                    CloseHandle(thdhnd[i]);
               }
          }
          delete[] sd;
          delete[] thdhnd;
          delete[] thdid;
          WSACleanup();
          cprintf("%d connection(s) was created in %.3f second(s)\n",succnum,(double)((stop-start)/CLOCKS_PER_SEC));
          cprintf("%d connection(s) was failed to create\n\n",reqnum-succnum);
     }
     else {
          cprintf("requester [destIP] [destPORT] [number]\n\n");
          cprintf("destIP          Destination IP of request to create\n");
          cprintf("destPORT     Destination PORT of request to create\n");
          cprintf("number          Number of request to create (optional, default=1)\n");
     }
}

DWORD WINAPI thdproc(LPVOID lpParam) {
     SOCKET *sptr=(SOCKET*)lpParam;

     if((*sptr=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP))!=INVALID_SOCKET) {
          if((connect(*sptr,(sockaddr*)&addr,sizeof(sockaddr_in)))==0) {
               //disconnect
               succnum++;
          }
          else cprintf("SOCKET ERROR : %d\n",WSAGetLastError());
     }
     else cprintf("INVALID SOCKET : %d\n",WSAGetLastError());
     return 0;
}
post if you still waiting for answer, i have a multi-thread server written in C under unix platform.
ASKER CERTIFIED SOLUTION
Avatar of SpideyMod
SpideyMod

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial