I've written a socket server in Matlab and also a client program in Python.Both can be run properly respectively.The purpose is to send signals from Matlab to Python.But the problem is that they can never be connected to each other even though I use the same the host and port address.PLZ help me out.THX.
This is the server in Matlab:

function server(message, output_port, number_of_retries)

    import java.net.ServerSocket
    import java.io.*

    if (nargin <4)
        number_of_retries = 20; % set to -1 for infinite
    end
    retry             = 0;

    server_socket  = [];
    output_socket  = [];

    while true

        retry = retry + 1;

        try
            if ((number_of_retries > 0) && (retry > number_of_retries))
                fprintf(1, 'Too many retries\n');
                break;
            end

            fprintf(1, ['Try %d waiting for client to connect to this ' ...
                        'host on port : %d\n'], retry, output_port);

            % wait for 1 second for client to connect server socket
            server_socket = ServerSocket(output_port);
            server_socket.setSoTimeout(1000);

            output_socket = server_socket.accept;

            fprintf(1, 'Client connected\n');

            output_stream   = output_socket.getOutputStream;
            d_output_stream = DataOutputStream(output_stream);

            % output the data over the DataOutputStream
            % Convert to stream of bytes
            fprintf(1, 'Writing %d bytes\n', length(message))
            d_output_stream.writeBytes(char(message));
            d_output_stream.flush;
            
            % clean up
            server_socket.close;
            output_socket.close;
            break;
            
        catch
            if ~isempty(server_socket)
                server_socket.close
            end

            if ~isempty(output_socket)
                output_socket.close
            end

            % pause before retrying
            pause(1);
   end
end
end

This is the client program in Python:

from socket import *
from java.io import *
host = "localhost"
port = 3011
buf = 1024
addr = (host,port)

# Create socket and bind to address
UDPSock = socket(AF_INET,SOCK_DGRAM)
UDPSock.bind(addr)
# Receive messages

while 1:
    input_stream   = UDPSock.recvfrom(buf)
    if not input_stream:
        print "Client has exited!"
        break
    else:
        print "\nReceived message '", input_stream, "'"
        file = open('C:\Documents and Settings\Hurricane17\Desktop\HIWI_Fraunhofer\Data1.txt', 'r+')
        message = str(input_stream)
        writebytes=file.write(input_stream)
        file.close()
# Close sockets
UDPSock.close()

If your OS has some kind of socket support to communicate between processes then you can use that. You could also do something like letting an SQL file take care of the locking issues, and have one or both programs write to file and the other check periodically for updates, but that is definitely a hack.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.