The Code-Bin
Links
Home
Add your code!
All Listings
About
Latest Entry
Featured Scripts
Author's Website
Latest Entries
FFMPEG Thumbnail Scr...
PHP, 0.8KB
Jul. 29, 10:24pm
John
Z80 Assembler, 190 bytes
Feb. 17, 3:36am
John
Z80 Assembler, 176 bytes
Sep. 13, 2:19am
John
Z80 Assembler, 77 bytes
Sep. 13, 2:18am
John
Z80 Assembler, 209 bytes
Sep. 13, 2:17am
untitled C Code
Posted by: vanquish | October 5, 2010 @ 11:20am
C Code
[
Download
]
#include <unistd.h> #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #include <stdio.h> #include <stdlib.h> #include <time.h> /* the port users will be connecting to */ #define MYPORT 3440 /* how many pending connections queue will hold */ #define BACKLOG 10 int createConnectedSocket(char* local_ip, int local_port, char* remote_ip, int remote_port); int main(int argc, char *argv[]) { /* listen on sock_fd */ int sockfd, new_fd; /* my address information, address where I run this program */ struct sockaddr_in local_addr; /* remote address information */ struct sockaddr_in remote_addr; int sin_size; int testSocket = createConnectedSocket(argv[1], argv[2], argv[3], argv[4]); sleep(30000); close(testSocket); return 0; } int createConnectedSocket(char* local_ip, int local_port, char* remote_ip, int remote_port) { int tempSocket = socket(AF_INET, SOCK_DGRAM, 0); if (tempSocket < 0) return tempSocket; // Create local socket struct sockaddr_in localsocket; memset(&localsocket, 0, sizeof(localsocket)); localsocket.sin_family = AF_INET; localsocket.sin_addr.s_addr = local_ip; localsocket.sin_port = htons(local_port); if (bind(tempSocket, (struct sockaddr *) &localsocket, sizeof(localsocket))) return -1; // Create static remote connection struct sockaddr_in remotesocket; memset(&remotesocket, 0, sizeof(remotesocket)); remotesocket.sin_family = AF_INET; remotesocket.sin_addr.s_addr = inet_addr(remote_ip); remotesocket.sin_port = htons(remote_port); if (connect(tempSocket, (struct sockaddr *) &remotesocket, sizeof(remotesocket)) < 0) return -1; return tempSocket; }
Syntax Highlighting
[
Open in new window
]
Author Comments
none
Rating
4.52 / 8
54 Votes
http://codebin.yi.org/876
page generated in 0.01 seconds