TCP/IP Application Development
From Computing and Software Wiki
(→Examples) |
|||
| Line 12: | Line 12: | ||
==Examples== | ==Examples== | ||
| + | <code lang="c"> | ||
| + | #include <string.h> | ||
| + | #include <sys/types.h> | ||
| + | #include <sys/socket.h> | ||
| + | #include <netinet/in.h> | ||
| + | #include <arpa/inet.h> | ||
| + | #include <stdio.h> | ||
| + | |||
| + | #define SERVERPORT 8888 | ||
| + | #define SERVERADDRESS "192.168.1.105" | ||
| + | |||
| + | int main() | ||
| + | { | ||
| + | int servSock, bytes_sent, sin_size; | ||
| + | |||
| + | char msg[10]; | ||
| + | |||
| + | struct sockaddr_in serverAddress; // server's address and port information | ||
| + | |||
| + | struct hostent *host; //host information | ||
| + | |||
| + | |||
| + | if ((servSock = socket(PF_INET, SOCK_STREAM, 0)) < 0) | ||
| + | printf("socket() FAILED"); | ||
| + | |||
| + | serverAddress.sin_family = AF_INET; | ||
| + | serverAddress.sin_port = htons(SERVERPORT); | ||
| + | serverAddress.sin_addr.s_addr = inet_addr(SERVERADDRESS); | ||
| + | memset(serverAddress.sin_zero, '\0', sizeof serverAddress.sin_zero); | ||
| + | |||
| + | connect(servSock, (struct sockaddr *)&serverAddress, sizeof serverAddress); | ||
| + | |||
| + | while(1) | ||
| + | { | ||
| + | scanf("%s", msg); | ||
| + | send(servSock, msg, sizeof (msg), 0); | ||
| + | if(strcmp(msg, "exit") == 0) | ||
| + | break; | ||
| + | } | ||
| + | close(servSock); | ||
| + | |||
| + | return 1; | ||
| + | } | ||
| + | </code> | ||
==References== | ==References== | ||
Revision as of 02:30, 8 April 2008
Contents |
Introduction
TCP/IP Applications are Applications based on the TCP/IP protocol suite. It is part of the Application Layer of the Layered Network Model also known as OSI.
Client/Server Model
Best Practices
Security Issues
Examples
- include <string.h>
- include <sys/types.h>
- include <sys/socket.h>
- include <netinet/in.h>
- include <arpa/inet.h>
- include <stdio.h>
- define SERVERPORT 8888
- define SERVERADDRESS "192.168.1.105"
int main() { int servSock, bytes_sent, sin_size;
char msg[10];
struct sockaddr_in serverAddress; // server's address and port information
struct hostent *host; //host information
if ((servSock = socket(PF_INET, SOCK_STREAM, 0)) < 0)
printf("socket() FAILED");
serverAddress.sin_family = AF_INET; serverAddress.sin_port = htons(SERVERPORT); serverAddress.sin_addr.s_addr = inet_addr(SERVERADDRESS); memset(serverAddress.sin_zero, '\0', sizeof serverAddress.sin_zero);
connect(servSock, (struct sockaddr *)&serverAddress, sizeof serverAddress);
while(1) { scanf("%s", msg); send(servSock, msg, sizeof (msg), 0); if(strcmp(msg, "exit") == 0) break; } close(servSock);
return 1; }
