下面是server端源码:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <net/if.h>
#include <time.h>
void error_handling(char *msg);
void getLocalIP(char *ip){
int inet_sock;
struct ifreq ifr;
inet_sock = socket(AF_INET, SOCK_DGRAM, 0);
strcpy(ifr.ifr_name, "eth0");
ioctl(inet_sock, SIOCGIFADDR, &ifr);
strcpy(ip, inet_ntoa(((struct sockaddr_in *)&ifr.ifr_addr)->sin_addr));
}
int main(int argc,char *argv[])
{
int serv_sock_fd;
int clnt_sock_fd;
struct sockaddr_in serv_addr;
struct sockaddr_in clnt_addr;
socklen_t clnt_addr_size;
char message[1024];
int rcv_len;
time_t timer;//time_t就是long int 类型
struct tm *tblock;
char local_ip[32]={0};
getLocalIP(local_ip);
printf("\r\ngetLocalIP=%s\r\n",local_ip);//获取本机IP地址
// if (argc != 2)
// {
// printf("Usage: %s <port>\n", argv[0]);
// exit(1);
// }
serv_sock_fd = socket(AF_INET, SOCK_STREAM, 0);
if (serv_sock_fd == -1)
error_handling("sock() error");
memset(&serv_addr, 0, sizeof(serv_addr)) ;
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = htonl(INADDR_ANY);
serv_addr.sin_port = 8080;//htons(atoi(argv[1]));//监听端口
if (bind(serv_sock_fd, (struct sockaddr*)&serv_addr, sizeof(serv_addr)) == -1)
error_handling("bind() error");
if (listen(serv_sock_fd, 5) == -1)
error_handling("listen() error");
while(1)
{
clnt_addr_size = sizeof(clnt_addr);
clnt_sock_fd = accept(serv_sock_fd, (struct sockaddr*)&clnt_addr, &clnt_addr_size);
if (clnt_sock_fd == -1)
error_handling("accept() error");
rcv_len = read(clnt_sock_fd, message, sizeof(message) - 1);
timer = time(NULL);
tblock = localtime(&timer);
printf("%sMessage send: %s\n", asctime(tblock),message);
write(clnt_sock_fd, message, sizeof(message));//将收到的数据发送出去
close(clnt_sock_fd);
usleep(1000);
}
close(serv_sock_fd);
printf("\r\nserver end.\r\n");
return 0;
}
void error_handling(char *message)
{
fputs(message, stderr);
fputc('\n', stderr);
exit(1);
}
下面是client端源码:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <net/if.h>
#include <time.h>
void error_handling(char *msg);
void getLocalIP(char *ip){
int inet_sock;
struct ifreq ifr;
inet_sock = socket(AF_INET, SOCK_DGRAM, 0);
strcpy(ifr.ifr_name, "eth0");
ioctl(inet_sock, SIOCGIFADDR, &ifr);
strcpy(ip, inet_ntoa(((struct sockaddr_in *)&ifr.ifr_addr)->sin_addr));
}
int main(int argc,char *argv[])
{
int sock_fd;
struct sockaddr_in serv_addr;
char message[1024] = {0};
int rcv_len,package=0;
time_t timer;//time_t就是long int 类型
struct tm *tblock;
// if (argc != 3)
// {
// printf("Usage: %s <IP> <port>\n", argv[0]);
// exit(1);
// }
char local_ip[32];
getLocalIP(local_ip);
printf("\r\ngetLocalIP=%s\r\n",local_ip);//获取本机IP地址,作为远端服务器IP地址
while(1)
{
sock_fd = socket(AF_INET, SOCK_STREAM, 0);
if (sock_fd == -1)
error_handling("sock() error");
memset(&serv_addr, 0, sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = inet_addr(local_ip);//server's IP addr
serv_addr.sin_port = 8080;//htons(atoi(argv[2]));//server's port
if (connect(sock_fd, (struct sockaddr*)&serv_addr, sizeof(serv_addr)) == -1)
error_handling("connect() error!");
sprintf(message,"This is a sample TCP client!\t%d",package++);
write(sock_fd, message, sizeof(message));//将收到的数据发送出去
usleep(10000);
rcv_len = read(sock_fd, message, sizeof(message) - 1);
if (rcv_len == -1)
error_handling("read() error!");
timer = time(NULL);
tblock = localtime(&timer);
printf("%sMessage receved[%d]: %s\n", asctime(tblock),rcv_len,message);
close(sock_fd);
usleep(10000);
}
printf("\r\nclient end.\r\n");
return 0;
}
void error_handling(char *message)
{
fputs(message, stderr);
fputc('\n', stderr);
exit(1);
}
分别gcc编译后,先运行server,再运行client,会不停有数据打印,恭喜成功了。