服务端的程序,修改权限至777 ,先运行服务端程序,再运行客户端程序,效果为客户端输入字符,服务端将其打印出来,输入end结束
#include <stdio.h>
#include <netdb.h>
#include <unistd.h>
#include <netinet/in.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/types.h>
#define MAX 10*1024
#define PORT 6666
// Driver function
int main()
{
char buff[MAX];
int n;
int sockfd, connfd, len;
struct sockaddr_in server, client;
// socket create and verification
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd == -1) {
printf("socket creation failed...
");
exit(0);
}
printf("socket successfully created..
");
bzero(&server, sizeof(server));
// assign IP, PORT
server.sin_family = AF_INET;
server.sin_addr.s_addr = htonl(INADDR_ANY);
server.sin_port = htons(PORT);
// binding newly created socket to given IP and verification
if ((bind(sockfd, (struct sockaddr*)&server, sizeof(server))) != 0) {
printf("socket bind failed...
");
exit(0);
}
printf("socket successfully binded..
");
// now server is ready to listen and verification
if ((listen(sockfd, 5)) != 0) {
printf("Listen failed...
");
exit(0);
}
printf("server listening...
");
len = sizeof(client);
// accept the data packet from client and verification
connfd = accept(sockfd, (struct sockaddr*)&client, &len);
if (connfd < 0) {
printf("server acccept failed...
");
exit(0);
}
printf("server acccept the client...
");
// infinite loop for chat
while(1) {
bzero(buff, MAX);
// read the messtruct sockaddrge from client and copy it in buffer
if (read(connfd, buff, sizeof(buff)) <= 0) {
printf("client close...
");
close(connfd);
break;
}
// print buffer which contains the client contents
printf("from client: %s
", buff);
// if msg contains "Exit" then server exit and chat ended.
if (strncmp("exit", buff, 4) == 0) {
printf("server exit...
");
close(connfd);
break;
}
}
// After chatting close the socket
close(sockfd);
exit(0);
}
客户端:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#define HOST "192.168.0.217" // 根据你服务器的IP地址修改
#define PORT 6666 // 根据你服务器进程绑定的端口号修改
#define BUFFER_SIZ (4 * 1024) // 4k的数据区域
int main(void)
{
int sockfd, ret;
struct sockaddr_in server;
char buffer[BUFFER_SIZ]; //用于保存输入的文本
// 创建套接字描述符
if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
printf("create an endpoint for communication fail!
");
exit(1);
}
bzero(&server, sizeof(server));
server.sin_family = AF_INET;
server.sin_port = htons(PORT);
server.sin_addr.s_addr = inet_addr(HOST);
// 建立TCP连接
if (connect(sockfd, (struct sockaddr *)&server, sizeof(struct sockaddr)) == -1) {
printf("connect server fail...
");
close(sockfd);
exit(1);
}
printf("connect server success...
");
while (1) {
printf("please enter some text: ");
fgets(buffer, BUFFER_SIZ, stdin);
//输入了end,退出循环(程序)
if(strncmp(buffer, "end", 3) == 0)
break;
write(sockfd, buffer, sizeof(buffer));
}
close(sockfd);
exit(0);
}
服务器IP应当根据实际进行修改。
获取IP地址的函数:
#include <stdio.h> #include <sys/types.h> #include <ifaddrs.h> #include <netinet/in.h> #include <string.h> #include <arpa/inet.h> int main (int argc, const char * argv[]) { struct ifaddrs * ifAddrStruct=NULL; struct ifaddrs * ifa=NULL; void * tmpAddrPtr=NULL; getifaddrs(&ifAddrStruct); for (ifa = ifAddrStruct; ifa != NULL; ifa = ifa->ifa_next) { if (!ifa->ifa_addr) { continue; } if (ifa->ifa_addr->sa_family == AF_INET) // check it is IP4 { // is a valid IP4 Address tmpAddrPtr=&((struct sockaddr_in *)ifa->ifa_addr)->sin_addr; char addressBuffer[INET_ADDRSTRLEN]; inet_ntop(AF_INET, tmpAddrPtr, addressBuffer, INET_ADDRSTRLEN); printf("%s IP4Func Address %s ", ifa->ifa_name, addressBuffer); } else if (ifa->ifa_addr->sa_family == AF_INET6) // check it is IP6 { // is a valid IP6 Address tmpAddrPtr=&((struct sockaddr_in6 *)ifa->ifa_addr)->sin6_addr; char addressBuffer[INET6_ADDRSTRLEN]; inet_ntop(AF_INET6, tmpAddrPtr, addressBuffer, INET6_ADDRSTRLEN); printf("%s IP6Func Address %s ", ifa->ifa_name, addressBuffer); } } if (ifAddrStruct!=NULL) { freeifaddrs(ifAddrStruct); } return 0; }