Issue : Socket Server And Client in different languages

  • Thread starter Thread starter Syed Babu
  • Start date Start date
S

Syed Babu

Guest
This is very related to my previous question. I have server written in tcl language which simply receives the string from the server and sends back another string to the client. The code looks like this:

proc ConxnAcceptCallBack {chan addr port} { ;# Make a proc to accept connections
puts "$addr:$port says [gets $chan]" ;# Receive a string
puts $chan "Server is made of tcl"
close $chan ;# Close the socket (automatically flushes)
} ;#
socket -server ConxnAcceptCallBack 20000 ;# Create a server socket
vwait forever ;# Enter the event loop

Im using the port as 20000.

I have written my client in C++ which is connected to the server. The issue is Im having send call where Im sending the string to the server.


#include "stdafx.h"
#include <iostream>
using namespace std;

#include <Winsock2.h>
#include <Windows.h>


int _tmain(int argc, _TCHAR* argv[])
{
int iResult = -1;
char szPath[128] = "";

WSADATA wsaData;

//Intialize the win socket dll by passing highest version of windows socket specification
WSAStartup(MAKEWORD(2, 2), &wsaData);

//Get the host name
gethostname(szPath, sizeof(szPath));

//Get the host by name and fetch the id address
hostent* pHostEnt = gethostbyname(szPath);
in_addr* pInAddress = (in_addr*)(*pHostEnt->h_addr_list);
char* szIPAddresss = inet_ntoa(*pInAddress);

//Create an unnamed socket for the server
int client_socket_fd = socket(AF_INET,SOCK_STREAM,0 );

//Connect the client to the port
sockaddr_in client_address;
client_address.sin_family = AF_INET;
client_address.sin_addr = *pInAddress;
client_address.sin_port = htons(20000);
int client_addr_len = sizeof(sockaddr_in);
iResult = connect(client_socket_fd, (struct sockaddr *)&client_address, client_addr_len);

if( iResult != 0 )
{
int iErrCode = WSAGetLastError();
cout<<iErrCode<<endl;
}

char* x = "This server is made of C++";
int a = 10;
DWORD nbBytesWritten = 0;
int ierr = send( client_socket_fd,(char*)x,strlen(x), 0);


//If there is closesocket call here, server is able to receive the string sent

//Otherwise the server goes to non-responding mode??????

DWORD numBytesRead = 0;
char buf[255];
ierr = recv( client_socket_fd,buf,255,0 );

closesocket(client_socket_fd );

WSACleanup();

return 0;
}



From the client side, the send call is succeeds by returning the number of characters written. But the server goes to non-responsive mode ( busy cursor keeps on rotates ).

But where as If I have closesocket call after the send, my server is able to receive the string sent from the client.

Is there any flushing issue?

But If I close the socket after the send call, how can I make use of the recv command ( to receive the string sent by the server ).

Continue reading...
 

Similar threads

P
Replies
0
Views
174
Policy standard local admin account with Active Di
P
A
Replies
0
Views
72
aaaabbbbccccddddeeeeffffgggg
A
Back
Top