Solved

Socket, C, Thread

Posted on 2002-07-03
8
1,370 Views
Last Modified: 2013-12-14
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.
0
Comment
Question by:anuravi
8 Comments
 
LVL 5

Expert Comment

by:nebeker
ID: 7128257
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....
0
 
LVL 1

Expert Comment

by:th94nn
ID: 7161325
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.
0
 
LVL 1

Expert Comment

by:bculver98
ID: 7165478
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
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 1

Expert Comment

by:bculver98
ID: 7165479
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
0
 
LVL 1

Expert Comment

by:bculver98
ID: 7165481
Sorry for the double posting ... I don't know how that happened
0
 

Expert Comment

by:Mrxlazuardin
ID: 7167608
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;
}
0
 
LVL 14

Expert Comment

by:kennethxu
ID: 7429688
post if you still waiting for answer, i have a multi-thread server written in C under unix platform.
0
 

Accepted Solution

by:
SpideyMod earned 0 total points
ID: 8279764
All,
I am unlocking this question in preparation for cleanup.  I will return in 7 days to finalize this question.  Please leave any recommendations for the final state of this question, I will take all recommendations into consideration.  Failing any feedback, I may decide in 7 days to delete or PAQ this question with no refund.  Thanks.

SpideyMod
Community Support Moderator @Experts Exchange
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
Here is a helpful source code for C++ Builder programmers that allows you to manage and manipulate HTML content from C++ code, while also handling HTML events like onclick, onmouseover, ... Some objects defined and used in this source include: …
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.
The viewer will learn how to use and create new code templates in NetBeans IDE 8.0 for Windows.

685 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question