Differences

This shows you the differences between two versions of the page.

wiki:study:prakt:tel [2005/07/18 20:15] (current)
Line 1: Line 1:
 +====== Telematik ======
 +  * tcp- client realisieren
 +  * tcp- forwarder realisieren
 +
 +===== tcp_client.c =====
 +<code c>
 +//simple tcp-client like nc
 +#include <sys/types.h>
 +#include <sys/socket.h>
 +#include <netinet/in.h>
 +
 +#include <stdio.h>
 +
 +int main () {
 + unsigned int tcp_remoteport = 7770;
 +
 + char tcp_remotehost[16] = "127.000.000.001";
 +
 + char buffer[256] = "";
 +
 + int client_socket_fd;
 +
 + struct sockaddr_in client_socket = { 0 };
 +
 +
 + //-----------------Client Part----------------------------------------------
 + //create a new socket
 + if ( (client_socket_fd=socket(PF_INET,SOCK_STREAM,IPPROTO_TCP)) == -1) {
 + printf("Client-error: can't create socket\n");
 + exit(0);
 + }
 +
 + //socket setup
 + client_socket.sin_family=AF_INET;
 + client_socket.sin_port = htons(tcp_remoteport);
 + client_socket.sin_addr.s_addr = inet_addr(tcp_remotehost);
 + //--------------------------------------------------------------------------
 +
 + if (connect(client_socket_fd,(struct sockaddr*) &client_socket,
 + sizeof(client_socket)) != -1) {
 + while (1) {
 + printf(">>");
 + scanf("%s", buffer);
 + sprintf(buffer,"%s\n",buffer);
 + write(client_socket_fd,buffer,sizeof(buffer)-1);
 +
 + if (read(client_socket_fd,buffer,sizeof(buffer)-1) != -1) {
 + printf("<<%s",buffer);
 + }
 + }
 + } else {
 + printf("Client-error: can't connect to %s:%d\n",tcp_remotehost,
 + tcp_remoteport);
 + }
 +}
 +</code>
 +
 +===== tcp_forwarder.c =====
 +<code c>
 +//simple tcp-forwarder
 +#include <sys/types.h>
 +#include <sys/socket.h>
 +#include <netinet/in.h>
 +
 +int main () {
 + unsigned int tcp_listenport = 7777;
 + unsigned int tcp_remoteport = 7770;
 +
 + char tcp_remotehost[16] = "127.000.000.001";
 + char tcp_serverinterface[16] = "000.000.000.000";
 +
 + char buffer[256] = "";
 +
 + int accepted_server_socket_len;
 + int server_socket_fd,accepted_server_socket_fd;
 +
 +
 + int client_socket_fd;
 +
 + int server_socket_size,client_socket_size;
 +
 + int on;
 +
 + struct sockaddr_in server_socket = { 0 };
 + struct sockaddr_in accepted_server_socket = { 0 };
 +
 + struct sockaddr_in client_socket = { 0 };
 +
 + //-----------------Server Part---------------------------------------------
 + //create a new socket
 + if ( (server_socket_fd=socket(PF_INET,SOCK_STREAM,IPPROTO_TCP)) == -1) {
 + printf("Server-error: can't create socket\n");
 + exit(0);
 + }
 +
 + //turn off bind adress
 + on=1;
 + if ( setsockopt(server_socket_fd,SOL_SOCKET,SO_REUSEADDR,(const char *) &on,
 + sizeof(on)) == -1)
 + printf("Server-warning: unable release binding\n",tcp_listenport);
 +
 +
 + //allow access from 0.0.0.0
 + server_socket.sin_addr.s_addr=inet_addr(tcp_serverinterface);
 + server_socket.sin_family=AF_INET;
 + server_socket.sin_port = htons(tcp_listenport);
 +
 + //bind to interface/ip
 + if ( bind(server_socket_fd,(struct sockaddr *) &server_socket,
 + sizeof(server_socket)) == -1) {
 + printf("Server-error: unable binding on port %d\n",tcp_listenport);
 + close(server_socket_fd);
 + exit(0);
 + }
 +
 + //listen
 + if ( listen(server_socket_fd,1) ) {
 + printf("Server-error: unable binding on port %d\n",tcp_listenport);
 + close(server_socket_fd);
 + exit(0);
 + }
 + //--------------------------------------------------------------------------
 +
 + //-----------------Client Part----------------------------------------------
 + //create a new socket
 + if ( (client_socket_fd=socket(PF_INET,SOCK_STREAM,IPPROTO_TCP)) == -1) {
 + printf("Client-error: can't create socket\n");
 + exit(0);
 + }
 +
 + //socket setup
 + client_socket.sin_family=AF_INET;
 + client_socket.sin_port = htons(tcp_remoteport);
 + client_socket.sin_addr.s_addr = inet_addr(tcp_remotehost);
 + //--------------------------------------------------------------------------
 +
 + while (1) {
 + accepted_server_socket_len=sizeof(accepted_server_socket_len);
 + if ( (accepted_server_socket_fd=accept(server_socket_fd,
 + (struct sockaddr *) &accepted_server_socket,
 + &accepted_server_socket_len)) != -1) {
 + if (connect(client_socket_fd,(struct sockaddr*) &client_socket,
 + sizeof(client_socket)) != -1) {
 + while (1) {
 + //transmit data from server part to client part
 + if (read(accepted_server_socket_fd,buffer,
 + sizeof(buffer)-1) != -1) {
 + printf(">>%s",buffer);
 + write(client_socket_fd,buffer,sizeof(buffer));
 + }
 + printf("data\n");
 + //transmit data from client part to server part
 + if (read(client_socket_fd,buffer,
 + sizeof(buffer)-1) != -1) {
 + printf("<<%s",buffer);
 + write(accepted_server_socket_fd,buffer,sizeof(buffer));
 + }
 + }
 + } else {
 + printf("Client-error: can't connect to %s:%d\n",tcp_remotehost,
 + tcp_remoteport);
 + }
 + }
 + close(accepted_server_socket_fd);
 + }
 +}
 +</code>
 
wiki/study/prakt/tel.txt · Last modified: 2005/07/18 20:15 (external edit)
 
Except where otherwise noted, content on this wiki is licensed under the following license:CC Attribution-Noncommercial-Share Alike 3.0 Unported
Recent changes RSS feed Donate Powered by PHP Valid XHTML 1.0 Valid CSS Driven by DokuWiki