I am attempting to make a proxy server, currently want to know why the iPhone I am testing it with sends a CONNECT request, gets the response of "HTTP 200 Connection Established" and usually never responds after that.
(sometimes it will respond instantly with "0" or NULL)
I am not actually connecting to where the iPhone wants to go, but I am sending back a response that indicates the proxy has connected
Terminal Output:
./proxy 9000
At this point I enter my local ip in the iPhone proxy settings
Example output:
REC:
CONNECT clientmetrics.kik.com:443 HTTP/1.1
Host: clientmetrics.kik.com
User-Agent: Kik/14.4.0.11622 CFNetwork/897.15 Darwin/17.5.0
Connection: keep-alive
Proxy-Connection: keep-alive
Sent:
HTTP/1.1 200 Connection Established
REC:
// Typically \0 or a 0, or no recv takes place
Code:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<netdb.h>
#include<netinet/in.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<unistd.h>
if(argc == 2) {
int main(int argc, char* argv[]) {
system("clear");
// Set up socket, family, port, ip address
int serversocket = socket(AF_INET, SOCK_STREAM, 0);
struct sockaddr_in serverinfo;
serverinfo.sin_family = AF_INET;
serverinfo.sin_addr.s_addr = INADDR_ANY;
serverinfo.sin_port = htons(atoi(argv[1]));
// bind server info to socket
if(bind(serversocket, (struct sockaddr*)&serverinfo, sizeof(serverinfo)) == -1) {
printf("failed to bind port to socket, change port\n");
return -1;
}
// accept a connection
listen(serversocket, 1);
int clientsocket = accept(serversocket, NULL, NULL);
// recv and print client message
char buffer[2048];
memset(buffer, 0, sizeof(buffer));
if(recv(clientsocket, buffer, sizeof(buffer), 0) == -1) {
printf("Failed in recv function\n");
} else {
printf("REC\n%s\n", buffer);
}
// send Connection Established
char* sendbuffer = "HTTP/1.1 200 Connection Established";
if(send(clientsocket, sendbuffer, sizeof(sendbuffer), 0) == -1) {
printf("Failed in send function\n");
} else {
printf("Sent:\n%s\n", sendbuffer);
// Recv response from iPhone
memset(buffer, 0, sizeof(buffer));
if(recv(clientsocket, buffer, sizeof(buffer), 0) == -1) {
printf("Failed in recv function\n");
} else {
printf("REC:\n%s\n", buffer);
}
}
} else {
printf("Usage: ./proxy <port>\n");
return 0;
}
}
Was just messing around some more and found that the iPhone will never respond beyond my CONNECT request because the connection must be closed after the proxy responds with HTTP 200 - then the connection can be reestablished and the iphone will continue to send data
i develop a program where i make i HTTPS requests in multithreading.
All work fine, however i need toimpleent use of proxy.
At the moment, i have this function for HTTPS Request :
struct string_builder* webrequest(const char* DEST_IP, const int DEST_PORT, const char* REQUEST) {
// Initialize ssl libraries and error messages
SSL_CTX *ssl_ctx = SSL_CTX_new (SSLv23_client_method());
// create a socket
int sockfd = socket(PF_INET, SOCK_STREAM, 0);
if (sockfd == -1) {
perror("Unable to create socket");
return NULL;
}
// destination info
struct sockaddr_in dest_addr;
dest_addr.sin_family = AF_INET; // host byte order
dest_addr.sin_port = htons(DEST_PORT); // short, network port
dest_addr.sin_addr.s_addr = inet_addr(DEST_IP); // destination address
memset(&(dest_addr.sin_zero), '\0', 8); // zero out the rest of the struct
// connect to the server
int status = connect(sockfd, (struct sockaddr*) &dest_addr, sizeof(struct sockaddr_in));
if (status == -1) {
perror("Unable to connect to the server");
close(sockfd);
return NULL;
}
// create SSL connection and attach it to the socket
SSL *conn = SSL_new(ssl_ctx);
SSL_set_fd(conn, sockfd);
SSL_connect(conn);
// send an encrypted message
ssize_t sendsize = SSL_write(conn, REQUEST, strlen(REQUEST));
if (sendsize == -1) {
perror("Unable to send to the server");
{
char *buf = malloc(sizeof(char) * 256);;
u_long err;
while ((err = ERR_get_error()) != 0) {
ERR_error_string_n(err, buf, sizeof(buf));
printf("*** %s\n", buf);
}
free(buf);
}
SSL_shutdown(conn);
ERR_free_strings();
ERR_remove_state(0);
SSL_CTX_free(ssl_ctx);
close(sockfd);
return NULL;
}
size_t i = 1;
struct string_builder *result = NULL;
result = string_builder_init();
while (i) {
// receive the response
const int RESPONSE_SIZE = 512;
char *response = malloc(sizeof(char)*512);
ssize_t recsize = SSL_read(conn, response, RESPONSE_SIZE-1);
if (recsize == -1) {
perror("Unable to send to the server");
SSL_shutdown(conn);
ERR_free_strings();
ERR_remove_state(0);
SSL_CTX_free(ssl_ctx);
close(sockfd);
return NULL;
}
response[recsize] = '\0';
if (recsize <= 0)
{
free(response);
break;
}
else if (i > 2 && recsize > 6)
{
string_builder_pushs(result, response);
}
free(response);
++i;
}
// close ssl connection
SSL_shutdown(conn);
ERR_free_strings();
ERR_remove_state(0);
SSL_free(conn);
SSL_CTX_free(ssl_ctx);
close(sockfd);
return result;
}
I use socket/send/recv function for socket, openssl library for HTTPS requests.
All work fine, no leak without proxy, but when i try to use a "CONNECT command" to connect to google (and socket connexion to proxy host), i have an error when i want to send.
Thanks in advance.
I want to create an HTTP post request in C. I have success to send Get Method but, when I try with Post, program is just stopped.
Here the code :
#include<stdio.h>
#include<winsock2.h>
#include <string.h>
#pragma comment(lib,"ws2_32.lib") //Winsock Library
int main(int argc , char *argv[])
{
WSADATA wsa;
SOCKET s;
struct sockaddr_in server;
char *message , server_reply[2000];
int recv_size;
printf("\nInitialising Winsock...");
if (WSAStartup(MAKEWORD(2,2),&wsa) != 0)
{
printf("Failed. Error Code : %d",WSAGetLastError());
return 1;
}
printf("Initialised.\n");
//Create a socket
if((s = socket(AF_INET , SOCK_STREAM , 0 )) == INVALID_SOCKET)
{
printf("Could not create socket : %d" , WSAGetLastError());
}
printf("Socket created.\n");
server.sin_addr.s_addr = inet_addr("127.0.0.1");
server.sin_family = AF_INET;
server.sin_port = htons( 80 );
//Connect to remote server
if (connect(s , (struct sockaddr *)&server , sizeof(server)) < 0)
{
puts("connect error");
return 1;
}
puts("Connected");
// Work well with Get Method
// strcat(message, "GET /voleur/receiver.php?arg1=Hello HTTP/1.1\n");
strcat(message, "POST /voleur/receiver.php HTTP/1.1\n");
strcat(message, "Host: 127.0.0.1\n");
strcat(message, "Connection: close\n");
strcat(message, "Content-Type:application/octet-stream\n");
strcat(message, "Content-Encoding:binary\n");
strcat(message, "Content-Length:16\n");
strcat(message, "Accept-Charset: utf-8\n\n");
if( send(s , message , strlen(message) , 0) < 0)
{
puts("Send failed");
return 1;
}
// My argument
char *data = "arg1=Hello";
send(s , data , strlen(data), 0);
puts("Data Send\n");
//Receive a reply from the server
if((recv_size = recv(s , server_reply , 2000 , 0)) == SOCKET_ERROR)
{
puts("recv failed");
}
puts("Reply received\n");
//Add a NULL terminating character to make it a proper string before printing
server_reply[recv_size] = '\0';
puts(server_reply);
return 0;
}
First:
You should reformat your HTTP header:
strcat(message, "Content-Type:application/octet-stream\n");
strcat(message, "Content-Encoding:binary\n");
strcat(message, "Content-Length:16\n");
should be
strcat(message, "Content-Type: application/octet-stream\n");
strcat(message, "Content-Encoding: binary\n");
strcat(message, "Content-Length: 16\n");
Second:
As far as I can see, your problem lies within the following lines:
strcat(message, "Content-Length: 16\n");
[...]
char *data = "arg1=Hello";
send(s , data , strlen(data), 0);
because you declare there will be 16 bytes of data - but you only send 10 bytes, which will cause the server to wait for the remaining 6 bytes.
I want to map two ports (anything goes to port x goes to port y and visa versa )with a C program, I wrote this program but it does not work.
This is my code:
int recv_all_nonblock(int sockfd,char* buff,int buffersize)
{
int numbytes;
if ((numbytes = recv(sockfd, buff, buffersize-1, MSG_NOSIGNAL|MSG_DONTWAIT)) <= 0)
{
perror("recv");
}
else if( numbytes>0)
{
buff[numbytes] = '\0';
}
return numbytes;
}
int sendall(int sockfd, char *buf, int *len)
{
int total = 0; // how many bytes we've sent
int bytesleft = *len; // how many we have left to send
int n=0;
while(total < *len)
{
n = send(sockfd, buf+total, bytesleft, MSG_NOSIGNAL|MSG_DONTWAIT);
if (n == -1) { break; }
total += n;
bytesleft -= n;
}
*len = total; // return number actually sent here
return n==-1?-1:0; // return -1 on failure, 0 on success
}
int Connect_To_Remote(char *Addr,char* PORT)
{
int sockfd;
struct addrinfo hints, *servinfo=NULL, *p=NULL;
int rv;
char s[INET6_ADDRSTRLEN];
memset(&hints, 0, sizeof hints);
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
while (p==NULL)
{
sleep(5);
while((rv = getaddrinfo(Addr, PORT, &hints, &servinfo)) != 0)
{
fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(rv));
sleep(5);
}
for(p = servinfo; p != NULL; p = p->ai_next)
{
if ((sockfd = socket(p->ai_family, p->ai_socktype,p->ai_protocol)) == -1)
{
perror("client: socket");
continue;
}
if (connect(sockfd, p->ai_addr, p->ai_addrlen) == -1)
{
close(sockfd);
perror("client: connect");
continue;
}
break;
}
if (p == NULL)
{
fprintf(stderr, "client: failed to connect\n");
}
}
inet_ntop(p->ai_family, get_in_addr((struct sockaddr *)p->ai_addr),s, sizeof s);
printf("client: connecting to %s\n", s);
freeaddrinfo(servinfo); // all done with this structure
return sockfd;
}
int recv_all(int sockfd,char* buff,int buffersize)
{
int numbytes;
fd_set readfd_set;
struct timeval recvwait;
recvwait.tv_sec=10;
recvwait.tv_usec=0;
FD_ZERO(&readfd_set);
FD_SET(sockfd, &readfd_set);
if(select(sockfd+1, &readfd_set, NULL, NULL, &recvwait) <= 0)
{
perror("wait for recieve error:");
}
else
{
if ((numbytes = recv(sockfd, buff, buffersize-1, MSG_NOSIGNAL|MSG_DONTWAIT)) == -1)
{
perror("recv");
}
else if( numbytes>0)
{
buff[numbytes] = '\0';
}
return numbytes;
}
return 0;
}
void *Port_Mapper()
{
int sockfd,newfd; // listen on sock_fd, new connection on new_fd
struct addrinfo hints, *servinfo, *tmpaddrinfo;
struct sockaddr_storage their_addr; // connector's address information
socklen_t sin_size;
int rv;
int yes=1;
int ret=0;
memset(&hints, 0, sizeof (hints));
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
hints.ai_flags = AI_PASSIVE; // use my IP
while (1)
{
if ((rv = getaddrinfo(NULL, MapPort, &hints, &servinfo)) != 0)
{
fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(rv));
sleep(2);
}
else
{
break;
}
}
while (1)
{
for(tmpaddrinfo = servinfo; tmpaddrinfo != NULL; tmpaddrinfo = tmpaddrinfo->ai_next)
{
if ((sockfd = socket(tmpaddrinfo->ai_family,tmpaddrinfo->ai_socktype,tmpaddrinfo->ai_protocol)) == -1)
{
perror("server: socket");
continue;
}
if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &yes,sizeof(int)) == -1)
{
perror("setsockopt");
exit(1);
}
if (bind(sockfd, tmpaddrinfo->ai_addr, tmpaddrinfo->ai_addrlen) == -1) {
close(sockfd);
perror("server: bind");
continue;
}
break;
}
if (tmpaddrinfo == NULL)
{
fprintf(stderr, "server: failed to bind\n");
sleep(1);
}
else
{
break;
}
}
freeaddrinfo(servinfo); // all done with this structure
if (listen(sockfd, MAXLISTENQ) == -1)
{
perror("listen");
exit(1);
}
int bufpoint=0;
printf("server: waiting for connections...\n");
while(1)
{ // main accept() loop
char buff[MAX_SOCK_BUFFER];
char buff2[MAX_SOCK_BUFFER];
ret=0;
int sockfdweb=0;
sin_size = sizeof (their_addr);
printf("mapping server: going to accept connections...\n");
newfd = accept(sockfd, (struct sockaddr *)&their_addr, &sin_size);
printf("\n\nmapping server: connections accepted:%d\n",newfd);
sockfdweb=Connect_To_Remote("192.168.1.10","80");
if (sockfdweb<0)
{
printf("can not connect to %s\n","192.168.1.10");
break;
}
while(1)
{
memset(buff,0,sizeof(buff));
ret=recv_all_nonblock(newfd,buff,MAX_SOCK_BUFFER);
printf("recved from client1,%d\n",ret);
if (ret<=0)
{
perror("recieve error from browser:");
close(newfd);
sin_size = sizeof (their_addr);
printf("mapping server: going to accept connections...\n");
newfd = accept(sockfd, (struct sockaddr *)&their_addr, &sin_size);
ret=recv_all_nonblock(newfd,buff,MAX_SOCK_BUFFER);
printf("recved from client10,%d\n",ret);
if (ret<=0)
{
perror("recieve error from browser:");
close(newfd);
sin_size = sizeof (their_addr);
printf("mapping server: going to accept connections...\n");
newfd = accept(sockfd, (struct sockaddr *)&their_addr, &sin_size);
}
else if(ret>0)
{
printf("recved from client0\n");
printf("%s\n",buff);
if (sendall(sockfdweb,buff,&bufpoint)<0)
{
printf("can not send data to %s\n","192.168.1.10");
}
printf("send to 80,0\n");
}
}
else if(ret>0)
{
printf("recved from client\n");
printf("%s\n",buff);
if (sendall(sockfdweb,buff,&bufpoint)<0)
{
printf("can not send data to %s\n","192.168.1.10");
}
printf("send to 80\n");
}
memset(buff2,0,sizeof(buff2));
ret=recv_all_nonblock(sockfdweb,buff2,MAX_SOCK_BUFFER);
printf("recv from 80...%d\n",ret);
if (ret<=0)
{
close(sockfdweb);
sockfdweb=Connect_To_Remote("192.168.1.10","80");
if (sockfdweb<0)
{
printf("can not connect to %s\n","192.168.1.10");
break;
}
ret=recv_all_nonblock(sockfdweb,buff2,MAX_SOCK_BUFFER);
printf("recv from 80.9..%d\n",ret);
if (ret<=0)
{
close(sockfdweb);
sockfdweb=Connect_To_Remote("192.168.1.10","80");
if (sockfdweb<0)
{
printf("can not connect to %s\n","192.168.1.10");
break;
}
}
else if (ret>0)
{
printf("recved from 809\n");
if (sendall(newfd,buff2,&ret)<0)
{
printf("can not send data to %s\n","192.168.1.10");
}
printf("send to client9\n");
}
}
else if (ret>0)
{
printf("recved from 80\n");
if (sendall(newfd,buff2,&ret)<0)
{
printf("can not send data to %s\n","192.168.1.10");
}
printf("send to client\n");
}
}
}
return 0;
}
int main()
{
.
.
.
.
pthread_attr_init (&attr);
pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED);
pthread_create(&portmapper_threadid,&attr,Port_Mapper,NULL);
pthread_attr_destroy (&attr);
.
.
.
.
}
I putted some "printf" in Port_Mapper function and made some changes to trace what happens when I request a web in my browser like this:
http://127.0.0.1:8090/
and the program out put of refreshing two or three times of browser is this:
mapping server: connections accepted:5
client: connecting to 192.168.1.10
recved from client1,360
recved from client
GET /1/ HTTP/1.1
Host: 127.0.0.1:8090
User-Agent: Mozilla/5.0 (X11; Linux i686; rv:6.0.2) Gecko/20100101 Firefox/6.0.2
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,/;q=0.8
Accept-Language: en-us,en;q=0.5
Accept-Encoding: gzip, deflate
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
Connection: keep-alive
Cache-Control: max-age=0
send to 80
recv: Resource temporarily unavailable
recv from 80...-1
client: connecting to 192.168.1.10
recv: Resource temporarily unavailable
recv from 80.9..-1
client: connecting to 192.168.1.10
recv: Resource temporarily unavailable
recved from client1,-1
recieve error from browser:: Resource temporarily unavailable
mapping server: going to accept connections...
recved from client10,360
recved from client0
GET /1/ HTTP/1.1
Host: 127.0.0.1:8090
User-Agent: Mozilla/5.0 (X11; Linux i686; rv:6.0.2) Gecko/20100101 Firefox/6.0.2
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,/;q=0.8
Accept-Language: en-us,en;q=0.5
Accept-Encoding: gzip, deflate
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
Connection: keep-alive
Cache-Control: max-age=0
send to 80,0
recv: Resource temporarily unavailable
recv from 80...0
client: connecting to 192.168.1.10
recv: Resource temporarily unavailable
recv from 80.9..-1
client: connecting to 192.168.1.10
recv: Resource temporarily unavailable
recved from client1,-1
recieve error from browser:: Resource temporarily unavailable
mapping server: going to accept connections...
recv: Resource temporarily unavailable
recved from client10,-1
recieve error from browser:: Resource temporarily unavailable
mapping server: going to accept connections...
recv: Resource temporarily unavailable
recv from 80...0
client: connecting to 192.168.1.10
recv: Resource temporarily unavailable
recv from 80.9..-1
client: connecting to 192.168.1.10
recved from client1,332
recved from client
GET / HTTP/1.1
Host: 127.0.0.1:8090
User-Agent: Mozilla/5.0 (X11; Linux i686; rv:6.0.2) Gecko/20100101 Firefox/6.0.2
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,/;q=0.8
Accept-Language: en-us,en;q=0.5
Accept-Encoding: gzip, deflate
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
Connection: keep-alive
send to 80
recv: Resource temporarily unavailable
recv from 80...-1
client: connecting to 192.168.1.10
recv: Resource temporarily unavailable
recv from 80.9..-1
client: connecting to 192.168.1.10
recv: Resource temporarily unavailable
recved from client1,-1
recieve error from browser:: Resource temporarily unavailable
mapping server: going to accept connections...
as you can see the program receive data from browser correctly but when it sends the data to webserver and wants to receive data from web server it always gets error and does not receive any data from web server:
send to 80
recv: Resource temporarily unavailable
recv from 80...-1
I have to say that the web server works correctly with no problem.
Can any body tell me what is my problem????
Your code is much to complicated for such an easy task. It can be simplified quote a lot, see this (pseudo) code:
void mapper()
{
int server_socket = create_server_socket();
for (;;)
{
int incomming_socket = accept(server_socket);
int web_server_socket = connect_to_webserver();
make_socket_nonblocking(incomming_socket);
make_socket_nonblocking(web_server_socket);
for (;;)
{
int disconnected = 0;
FD_ZERO(&read_set);
FD_SET(incomming_socket, &read_set);
FD_SET(web_server_socket, &read_set);
select(max_socket + 1, &read_set, NULL, NULL, NULL);
if (FD_ISSET(incomming_socket, &read_set))
{
disonnected = recv_and_send(incomming_socket, web_server_socket);
}
if (!disconnected && FD_ISSET(web_server_socket, &read_set))
{
disconnected = recv_and_send(web_server_socket, incomming_socket);
}
if (disconnected)
break;
}
close(web_server_socket);
close(incomming_socket);
}
}
int recv_and_send(from_socket, to_socket)
{
char buffer[1024];
for (;;)
{
len = recv_all(from_socket, buffer, sizeof(buffer));
if (len < 0)
return 1; /* Disconnect */
if (len == 0)
break; /* No more to read for now */
if (send_all(to_socket, buffer, len) < 0)
return 1; /* Disconnect */
}
return 0;
}
int recv_all(socket_fd, char *buffer, const size_t buflen)
{
size_t total_recv = 0;
size_t remaining_len = buflen;
while (remaining_len > 0)
{
ssize_t len = recv(socket_fd, buffer + total_recv, remaining_len);
if (len < 0)
{
if (errno == EWOULDBLOCK)
break; /* No more to read at the moment */
return -1; /* An error */
}
if (len == 0)
break; /* Connection closed */
total_recv += len;
remaining_len -= len;
}
return total_recv;
}
[The rest I leave as an exercise.]
The main difference with your code, is that the main loop is much simpler. You don't need multiple accept calls (why do you need it?) or several different read and write functions. Parts of my code can be even further simplified, like the recv_all function.
It should also be noted that my code above only handles one connection at a time. One way to solve this is to create a new thread for each incoming connection, or use a more advance method using lists of connections and buffers, and select to multiplex between them and the listening socket.
I am trying to send a string HI to a server over UDP in a particular port and then to receive a response. However, after I try to get the response using recvfrom() I was stuck in blocking state. I tried using connected UDP but I got:
Error receiving in UDP: Connection refused
What could be the reasons for this? The server is not under my control, but I do know its working fine.
I have added the code
int sockfdudp;
char bufudp[MAXDATASIZE], port[6];
struct addrinfo hints, *servinfo, *p;
struct sockaddr_storage addr;
int rv;
char s[INET6_ADDRSTRLEN];
int bytes_recv, bytes_sent;
socklen_t len;
scanf("%s",port);
printf("UDP Port: %s \n", port);
// Start connecting to datagram server
memset(&hints, 0, sizeof hints);
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_DGRAM;
if ((rv = getaddrinfo(SERVER_NAME, port, &hints, &servinfo)) != 0) {
fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(rv));
return 1;
}
// loop through all the results and make a socket
for(p = servinfo; p != NULL; p = p->ai_next) {
if ((sockfdudp = socket(p->ai_family, p->ai_socktype,
p->ai_protocol)) == -1) {
perror("Creating datagram socket");
continue;
}
if (connect(sockfdudp, p->ai_addr, p->ai_addrlen) == -1) {
close(sockfdudp);
perror("Connecting stream socket");
continue;
}
break;
}
if (p == NULL) {
fprintf(stderr, "ClientUDP: failed to bind socket\n");
return 2;
}
freeaddrinfo(servinfo);
if ((bytes_sent = sendto(sockfdudp, UDP_MSG, strlen(UDP_MSG), 0, p->ai_addr, p->ai_addrlen)) == -1) {
perror("ClientUDP: Error sending data");
exit(1);
}
printf("Data %s sent\n", UDP_MSG );
len = sizeof(struct sockaddr_storage);
if ((bytes_recv = recvfrom(sockfdudp, bufudp, MAXDATASIZE-1, 0,(struct sockaddr*)&addr, &len)) == -1) {
perror("Error receiving in UDP");
exit(1);
}
printf("Bytes recv %d\n", bytes_recv);
bufudp[bytes_recv] = '\0';
printf("ClientUDP: Received\n %s \n",bufudp );
close(sockfdudp);
return 0;
Chances are your're sending something to a server who does not listen on that particular port.
That would cause an icmp message to be sent back , and your next recvfrom will return an error in the case where you connect the socket.
Check with tcpdump or wireshark what's going on on the wire.
My guess would be that your ip address is bad somehow, or the port is already in use somehow. UDP is connectionless, so there really isn't any "connection" to fail.