|
qCNC 0.82-alpha
|
00001 00041 #include <stdio.h> 00042 #include <stdlib.h> 00043 #include <string.h> 00044 #include <unistd.h> 00045 #include <arpa/inet.h> 00046 #include <sys/types.h> 00047 #include <netinet/in.h> 00048 #include <sys/socket.h> 00049 #include <QtGui> 00050 #include "NetWork.h" 00051 00052 00053 00054 int connectSocket(char *host, int port) { 00055 struct sockaddr_in dest; 00056 int sock = 999; 00057 sock = socket(AF_INET, SOCK_STREAM,0); 00058 00059 memset(&dest, 0, sizeof(dest)); /* zero the struct */ 00060 dest.sin_family = AF_INET; 00061 dest.sin_addr.s_addr = inet_addr(host); /* set destination IP number */ 00062 dest.sin_port = htons(port); /* set destination port number */ 00063 if ( connect(sock, (struct sockaddr *)&dest, sizeof(struct sockaddr)) < 0) { 00064 //QMessageBox::critical(this, "Error Socket connect", "Error Connecting to socket. Please check ethernet connection!"); 00065 return -1; 00066 } 00067 else 00068 return sock; 00069 00070 } 00071 protocol *read_socket (int *socket, protocol *temp) { 00072 protocol *buffer; 00073 int recleng = 0; 00074 00075 recleng = recv(*socket,temp,sizeof(protocol) ,0); 00076 if (recleng <= 0 ) { 00077 return (protocol *) NULL; 00078 } 00079 else { 00080 //temp = (const protocol *)buffer; 00081 } 00082 00083 } 00084 int closeSocket(int *sock) { 00085 int err = close(*sock); 00086 if (err <= 0) { 00087 // QMessageBox::critical("Error closing Socket","An error occured while closing socket. This socket cannot be used again, until you have restarted the system"); 00088 return -1; 00089 } 00090 else 00091 return 0; 00092 } 00093 int write_socket(int *sock, protocol *buffer) { 00094 int written = 0; 00095 written = send(*sock, buffer, sizeof(*buffer), 0); 00096 return written; 00097 00098 }