Python <-> C via sockets

anuradha.k.r at sify.com anuradha.k.r at sify.com
Fri Sep 26 01:03:04 EDT 2003


hi,
I 've already posted a message to this group but i think You guys can
provide a solution faster.My problem is similar ,my client side
(written in python) is not able to connect to  the server side
(written in C) in the same machine.
my server side works perfectly fine and it waits for a connection to b
established.However my client gives error "10061.connection
refused".Will send you the code of both server side and client side.:


server side:
------------
#include<stdio.h>
#include<winsock2.h>
#include <windows.h>

SOCKET recvSock,WinSocket,slisten;
WSADATA WSAData;
SOCKADDR_IN Acceptor;
SOCKADDR_IN Connector;
int TypeOfCon;
int Initialise();
SOCKET ListenSocket();
int RecvBuff(BYTE * RecdBuffer,int size);
void Close();
unsigned char * RecdBuffer;

int main()
{
	int flag;	
	flag = Initialise();
	slisten= ListenSocket();
	flag = RecvBuff(RecdBuffer,20);
	Close();
	return 1;
}

int Initialise()
{
	WSAStartup (MAKEWORD(1,1), &WSAData);
	WinSocket = socket (AF_INET/*2*/, SOCK_STREAM/*1*/, 0);
	recvSock = socket (AF_INET/*2*/, SOCK_STREAM/*1*/, 0);
	return 1;
}

SOCKET ListenSocket()
{
	int error;
	int sizeofaddr;
	sizeofaddr = sizeof(Acceptor);

	/*BOOL mcast ;
	mcast= TRUE;*/

	Acceptor.sin_addr.S_un.S_addr = htonl(INADDR_ANY);		
	Acceptor.sin_family = AF_INET;
	Acceptor.sin_port = 9999;

	bind(WinSocket,(const SOCKADDR *)&Acceptor,sizeof(Acceptor));
	error = GetLastError();
	if(error)
	{
		return 0;
	}

	listen(WinSocket,1);
	error = GetLastError();
	if(error)
	{
		return 0;
	}
	recvSock = accept((SOCKET)WinSocket,(SOCKADDR
*)&Acceptor,&sizeofaddr);

	error = GetLastError();
	if(error)
	{
		return 0;
	}
	return recvSock;

}

int RecvBuff(BYTE * Buffer,int size)
{
	int amount,error;

	amount = recv(recvSock,(char *)Buffer,size,0);
	error = GetLastError();
	if(error)
	{
		return 0;
	}
	else
		return 1;
}

void Close()
{
	closesocket(WinSocket);
	closesocket(recvSock);
}

client side:
-------------

import socket

#HOST = '130.10.5.38'    # The remote host
#PORT = 50007              # The same port as used by the server
try:
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
except socket.error:
    print 'socket not creadted'
try:
    s.connect(("130.10.5.38", 9999))
except socket.error,msg:
    print 'error in connect'
 
#s.send('Hello, world')
#data = s.recv(1024)
s.close()
#print 'Received', `data`

I'd gone thru the discussion above,so i guess probably the problem
with my code is also on the port address only.Pls help.
thanx,
AKR.


nick.keighley at marconi.com (Nick Keighley) wrote in message news:<8ad2cfb3.0309240628.dbbca33 at posting.google.com>...
> Eric Brunel <eric.brunel at pragmadev.N0SP4M.com> wrote in message news:<bkroq8$8ud$1 at news-reader1.wanadoo.fr>...
> > Nick Keighley wrote:
> > > I'm probably missing something rather obvious, but is there a known 
> > > problem with getting a Python based socket program to communicate with
> > > a C based socket program? A simple echo server written in Python
> > > (the example from Python in a Nutshell actually) will happily talk
> > > to a Python based client. If a C based client is substitued the connection
> > > is refused (or so the C client reports). The C client will talk to
> > > a C server.
> > 
> > Communications between C and Python via sockets definetly work: we do that 
> > every  day (no kidding ;-)
> > 
> > Can you post an example of your code?
> 
> by the time you read this the posted code may have appeared.
> A call to htons() (convert unsigned short to network byte order) 
> was ommitted on the C side.
> 
> this line:-
>      sin.sin_port = PORT;
> 
> should read:-
>     sin.sin_port = htons(PORT);
> 
> 
> thanks!




More information about the Python-list mailing list