回环网卡通信

#include <stdio.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <unistd.h>


int port = 8000;

int main() {
struct sockaddr_in sin;
struct sockaddr_in pin;
int sock_descriptor;
int temp_sock_descriptor;
socklen_t address_size;
char buf[16384];
int i, len;

sock_descriptor = socket(AF_INET, SOCK_STREAM, 0);
if (sock_descriptor == -1) {
perror("call to socket");
exit(1);
}

bzero(&sin, sizeof(sin));
sin.sin_family = AF_INET;
sin.sin_addr.s_addr = INADDR_ANY;
sin.sin_port = htons(port);

if (bind(sock_descriptor, (struct sockaddr *)&sin, sizeof(sin)) == -1) {
perror("call to bind");
exit(1);
}

if (listen(sock_descriptor, 20) == -1) {
perror("call to listen");
exit(1);
}

printf("Accepting connections ...\n");

while(1) {
temp_sock_descriptor =
accept(sock_descriptor, (struct sockaddr *)&pin,
&address_size);
if (temp_sock_descriptor == -1) {
perror("call to accept");
exit(1);
}

if (recv(temp_sock_descriptor, buf, 16384, 0) == -1) {
perror("call to recv");
exit(1);
}

printf("received from client:%s\n", buf);

// for this server example, we just convert the
// characters to upper case:

len = strlen(buf);
printf("len = %d\n", len);
for (i=0; i<len; i++) buf[i] = toupper(buf[i]);

if (send(temp_sock_descriptor, buf, len, 0) == -1) {
perror("call to send");
exit(1);
}

close(temp_sock_descriptor);

}
return 0;
}

#include <stdio.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <string.h>

char * host_name = "127.0.0.1"; // local host
int port = 8000;

main(int argc, char *argv[])
{
char buf[8192];
int sd;
struct sockaddr_in pin;
struct hostent *server_host_name;
char *str = "A default test string";

if (argc < 2) {
printf("Usage: 'text \"Any test string\"\n'");
printf("We will send a default test string.\n");
}
else {
str = argv[1];
printf("argv_str = %d\n", strlen(argv[1]));
str[strlen(argv[1])] = '\0';
printf("str_len = %d\n", strlen(str));
}

//返回对应于给定主机名的包含主机名字和地址信息的hostent结构指针
if ((server_host_name = gethostbyname(host_name)) == 0) {
printf("Error resolving local host\n");
exit(1);
}

bzero(&pin, sizeof(pin));
pin.sin_family = AF_INET;
pin.sin_addr.s_addr = htonl(INADDR_ANY);
pin.sin_addr.s_addr = ((struct in_addr *)(server_host_name->h_addr))->s_addr;
pin.sin_port = htons(port);

if ((sd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
printf("Error opening socket\n");
exit(1);
}

if (connect(sd, (void *)&pin, sizeof(pin)) == -1) {
printf("Error connecting to socket\n");
exit(1);
}

// NOTE: must send a carriage return at end of message:
printf("Sending message %s to web_server...\n", str);

if (send(sd, str, strlen(str)+1, 0) == -1) {
printf("Error in send\n");
exit(1);
}

printf("..sent message.. wait for response...\n");

if (recv(sd, buf, 8192, 0) == -1) {
printf("Error in receiving response from NLPserver\n");
exit(1);
}

printf("\nResponse from NLPserver:\n\n%s\n", buf);

close(sd);

}

posted @ 2015-09-25 10:01  周人假的  阅读(351)  评论(0编辑  收藏  举报