473,378 Members | 1,523 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

multi threading

3
hi i am using the below code for dummy multithreading application.

first function listens request and the thread started with thread function
but some client start receving data but some one not get the single respomnse back . can any one help me what's gona wong with this code

thanks






///////the server function//////////////////



void thread::startserver()
{

DWORD dwStatus;
WSAEVENT handles[1];
struct sockaddr_in cli_addr;
int clilen = sizeof(cli_addr);
memset(&cli_addr,0,sizeof(struct sockaddr));

printf("***********server started****************\n");
while (TRUE)
{

handles[0] = WSACreateEvent();
WSAEventSelect(m_hSockethndl, handles[0], FD_ACCEPT);
dwStatus = WSAWaitForMultipleEvents(1, handles, TRUE, 50, FALSE);
if (dwStatus == WSA_WAIT_EVENT_0)
{
SOCKET cli_socket;
cli_socket=-1;
cli_socket = accept(m_hSockethndl, (struct sockaddr *) &cli_addr, &clilen);

// set linger option
LINGER linger;
linger.l_onoff = 1;
linger.l_linger = 0;
setsockopt(cli_socket, SOL_SOCKET, SO_LINGER, (char *)&linger, sizeof(LINGER));
//setsockopt(cli_socket, IPPROTO_TCP, TCP_NODELAY, NULL, 0);

int flag = 1;
int result = setsockopt(cli_socket, /* socket affected */
IPPROTO_TCP, /* set option at TCP level */
TCP_NODELAY, /* name of option */
(char *) &flag, /* the cast is historical cruft */
sizeof(int)); /* length of option value */
if (result < 0)
{
printf("Client Socket No Delay Option Failed");
}

//printf("Thread started with the server socket=%d and client port=%d and ip=%s\n",cli_socket,cli_addr.sin_port,inet_ntoa(cl i_addr.sin_addr));
/*threadParmas thparam;
memset(&thparam,0,sizeof(threadParmas));
thparam.cli_addrth=cli_addr;
thparam.threadsocket=cli_socket;*/

//CreateThread(NULL, 0, StartThread, &thparam, 0, &dwThreadID);
CreateThread(NULL, 0, StartThread, &cli_socket, 0, &dwThreadID);
FD_SET(cli_socket, &set_sockets);
}
WSACloseEvent(handles[0]);
}
}





//////////////////the thread function////////////////////

DWORD WINAPI thread::StartThread (void * thparam)
{
/* struct threadParmas threadparam;
threadparam.threadsocket=((struct threadParmas *)thparam)->threadsocket;
threadparam.cli_addrth=((struct threadParmas *)thparam)->cli_addrth;*/

SOCKET socket_handle;
u_short Clientport;
//socket_handle=threadparam.threadsocket;
socket_handle=*(SOCKET *)thparam;
//Clientport=ntohs(threadparam.cli_addrth.sin_port);
char data[256];
char Resdata[512];
memset(data,0,256);
memset(Resdata,0,512);
int counter=1;
int bytes =1;

while(bytes)
{

WSAEVENT we = WSACreateEvent();
WSAEVENT handles[1];
handles[0] = we;
WSAEventSelect(socket_handle, we, FD_READ | FD_CLOSE);
//receiving data from client
for (int i = 0; i < WAIT_MULTIPLE; i++)
{
DWORD available = WSAWaitForMultipleEvents(1, handles, TRUE, WAIT_TIME, FALSE);
if (available == WSA_WAIT_EVENT_0)
{
if ((bytes = recv(socket_handle, data, 256, 0)) < 0)
{
int er = WSAGetLastError();
printf("Error=%d while receive data from socket %d\n",er,socket_handle);
bytes = 0;
break;
}
else
break;
}
}

WSACloseEvent(we);

strcpy_s(Resdata,512,"Response send Ack");

if(bytes)
{
//printf("socket %d Message %3d received client port=%d, ip=%s size=%d\n",socket_handle,counter,Clientport,inet_n toa(threadparam.cli_addrth.sin_addr),bytes);
//go to sleep for internal processing
//printf("socket %d Message %3d received size %d %s\n",socket_handle,counter,bytes,data);
Sleep(2000);

//sending the dummy response to the client
if (send(socket_handle, Resdata, strlen(Resdata), 0) != strlen(Resdata))
{
int er = WSAGetLastError();
printf("ERROR %d IN SENDING %d RESPONSE by socket =%d\n",er,counter,socket_handle);
return 0;
}
//printf("***Response %3d send by the server socket=%d, client port=%d, ip=%s\n",counter,socket_handle,Clientport,inet_nto a(threadparam.cli_addrth.sin_addr));
//printf("***Response %3d send by the server socket=%d\n",counter,socket_handle);
}
//keeping track how many packets this specific client sends
counter++;
}
return 1;
}
Jun 28 '07 #1
2 2185
ROLE1
3
hi i am using this code for simulating multithreading but some client didn't get even the single response back can smone one help


//main function for server listenincoming request and open newthread to
// communicate with that client
void thread::startserver()
{

DWORD dwStatus;
WSAEVENT handles[1];
struct sockaddr_in cli_addr;
int clilen = sizeof(cli_addr);
memset(&cli_addr,0,sizeof(struct sockaddr));

printf("***********server started****************\n");
while (TRUE)
{

handles[0] = WSACreateEvent();
WSAEventSelect(m_hSockethndl, handles[0], FD_ACCEPT);
dwStatus = WSAWaitForMultipleEvents(1, handles, TRUE, 50, FALSE);
if (dwStatus == WSA_WAIT_EVENT_0)
{
SOCKET cli_socket;
cli_socket=-1;
cli_socket = accept(m_hSockethndl, (struct sockaddr *) &cli_addr, &clilen);

// set linger option
LINGER linger;
linger.l_onoff = 1;
linger.l_linger = 0;
setsockopt(cli_socket, SOL_SOCKET, SO_LINGER, (char *)&linger, sizeof(LINGER));
//setsockopt(cli_socket, IPPROTO_TCP, TCP_NODELAY, NULL, 0);

int flag = 1;
int result = setsockopt(cli_socket, /* socket affected */
IPPROTO_TCP, /* set option at TCP level */
TCP_NODELAY, /* name of option */
(char *) &flag, /* the cast is historical cruft */
sizeof(int)); /* length of option value */
if (result < 0)
{
printf("Client Socket No Delay Option Failed");
}

//printf("Thread started with the server socket=%d and client port=%d and ip=%s\n",cli_socket,cli_addr.sin_port,inet_ntoa(cl i_addr.sin_addr));
/*threadParmas thparam;
memset(&thparam,0,sizeof(threadParmas));
thparam.cli_addrth=cli_addr;
thparam.threadsocket=cli_socket;*/

//CreateThread(NULL, 0, StartThread, &thparam, 0, &dwThreadID);
CreateThread(NULL, 0, StartThread, &cli_socket, 0, &dwThreadID);
FD_SET(cli_socket, &set_sockets);
}
WSACloseEvent(handles[0]);
}
}
//thread function

DWORD WINAPI thread::StartThread (void * thparam)
{
/* struct threadParmas threadparam;
threadparam.threadsocket=((struct threadParmas *)thparam)->threadsocket;
threadparam.cli_addrth=((struct threadParmas *)thparam)->cli_addrth;*/

SOCKET socket_handle;
u_short Clientport;
//socket_handle=threadparam.threadsocket;
socket_handle=*(SOCKET *)thparam;
//Clientport=ntohs(threadparam.cli_addrth.sin_port);
char data[256];
char Resdata[512];
memset(data,0,256);
memset(Resdata,0,512);
int counter=1;
int bytes =1;

while(bytes)
{

WSAEVENT we = WSACreateEvent();
WSAEVENT handles[1];
handles[0] = we;
WSAEventSelect(socket_handle, we, FD_READ | FD_CLOSE);
//receiving data from client
for (int i = 0; i < WAIT_MULTIPLE; i++)
{
DWORD available = WSAWaitForMultipleEvents(1, handles, TRUE, WAIT_TIME, FALSE);
if (available == WSA_WAIT_EVENT_0)
{
if ((bytes = recv(socket_handle, data, 256, 0)) < 0)
{
int er = WSAGetLastError();
printf("Error=%d while receive data from socket %d\n",er,socket_handle);
bytes = 0;
break;
}
else
break;
}
}

WSACloseEvent(we);

strcpy_s(Resdata,512,"Response send Ack");

if(bytes)
{
//printf("socket %d Message %3d received client port=%d, ip=%s size=%d\n",socket_handle,counter,Clientport,inet_n toa(threadparam.cli_addrth.sin_addr),bytes);
//go to sleep for internal processing
//printf("socket %d Message %3d received size %d %s\n",socket_handle,counter,bytes,data);
Sleep(2000);

//sending the dummy response to the client
if (send(socket_handle, Resdata, strlen(Resdata), 0) != strlen(Resdata))
{
int er = WSAGetLastError();
printf("ERROR %d IN SENDING %d RESPONSE by socket =%d\n",er,counter,socket_handle);
return 0;
}
//printf("***Response %3d send by the server socket=%d, client port=%d, ip=%s\n",counter,socket_handle,Clientport,inet_nto a(threadparam.cli_addrth.sin_addr));
//printf("***Response %3d send by the server socket=%d\n",counter,socket_handle);
}
//keeping track how many packets this specific client sends
counter++;
}
return 1;
}
Jun 28 '07 #2
r035198x
13,262 8TB
Please always wrap your code in code tags for easier readability.
And don't double post.
Threads merged.
Jun 28 '07 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

77
by: Jon Skeet [C# MVP] | last post by:
Please excuse the cross-post - I'm pretty sure I've had interest in the article on all the groups this is posted to. I've finally managed to finish my article on multi-threading - at least for...
5
by: hari | last post by:
HI, I am trying multi threading in asp.net , but i could not do it. We do a daily Run in our company for calculations purpose etc.. we use SQl Stored Procedures to do all the process at back end....
13
by: notregister | last post by:
my program have as many as 8 threads running at a same time. can different threads open a same file at the same time? let say thread1 open a file abc.txt at the same time with thread 2, both doing...
2
by: NiponW | last post by:
Hi, I have SQL SERVER 2000 SP4 Enterprise , Windows 2003 Enterprise on Xeon 4 Processors (now with multi-threading CPU) and I have questions which seem weirds to me (used to have the same...
2
by: Juuso Hukkanen | last post by:
I need a list of multithreading unsafe C (C99) functions/features. comp.programming.threads provided an initial list of C:ish functions, with following ANSI C functions: asctime, gmtime,...
2
by: Ivan | last post by:
I have a class Foo which have two property. I have a thread that do some process and update class Foo int B. I have a datagridview on main form. this datagridview data source to this class Foo and...
1
by: vishal | last post by:
hi dear i am working on .net(1.1) web service and due to delay in response i made it multi threaded but when i passed a object in function which is called on a new thread, it is throwing...
0
ammoos
by: ammoos | last post by:
hi friends pls help me.. i got an assignment which i feel very difficult to me.. i dont have more knowledge about multi-threading in .net... the assignment details is below.... pls help me... i...
14
by: Akihiro KAYAMA | last post by:
Hi all. I found cooperative multi-threading(only one thread runs at once, explicit thread switching) is useful for writing some simulators. With it, I'm able to be free from annoying mutual...
5
by: George Maicovschi | last post by:
As multi-threading is not built in PHP I've been using a hack letting the Apache server handle the multi-threading issues, but I'm really curious of other approaches to this issue. If anyone has...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.