大家好,又见面了,我是你们的朋友全栈君。
只有今天贴出代码,明天看才知道自己有多么傻。
单线程,一对一聊天,混搭风格编程,函数乱入不解释……
/*
* Chat on Linux Terminal–alpha
* Worte by Jimmy’s team@uestc
* 2011-2-23
*
* This is the sorce code of client
* Some BUGS still unsloved, but we are trying our best to debug
* Be sure that your system’s port “1234” is not busy!
*
* */
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define SERVPORT 1234
#define MAX_DATA_SIZE 1024
int main(int argc, char *argv[]) {
int sockfd, sendBytes,recvBytes;
char sendBuf[MAX_DATA_SIZE],recvBuf[MAX_DATA_SIZE];
struct hostent *host;
struct sockaddr_in servAddr;
if(argc != 2) {
fprintf(stderr,”usage:./client [hostname]”);
exit(1);
}
/*translate the address*/
if((host = gethostbyname(argv[1])) == NULL) {
perror(“fail to get host by name”);
exit(1);
}
printf(“Success to get host by name…\n”);
/*establish a socket*/
if((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
perror(“fail to establish a socket”);
exit(1);
}
printf(“Success to establish a socket…\n”);
/*init sockaddr_in*/
servAddr.sin_family = AF_INET;
servAddr.sin_port = htons(SERVPORT);
servAddr.sin_addr = *((struct in_addr *)host -> h_addr);
bzero(&(servAddr.sin_zero), 8);
/*connect the socket*/
if(connect(sockfd, (struct sockaddr *)&servAddr, sizeof(struct sockaddr_in)) == -1) {
perror(“fail to connect the socket”);
exit(1);
}
printf(“Success to connect the socket…\n”);
printf(“\033[40;32mWelcome to join %s!\033[1m\n”, inet_ntoa(servAddr.sin_addr));//include color set
while(1) {
/*send datas to server*/
printf(“Client:”);
gets(sendBuf);
if((sendBytes = send(sockfd, sendBuf, strlen(sendBuf), 0)) != strlen(sendBuf)) {
perror(“fail to send datas”);
exit(1);
}
printf(“(Success to send data!)\n”);
memset(sendBuf, 0x00, MAX_DATA_SIZE);
/*receive datas from server*/
if((recvBytes = recv(sockfd, recvBuf, MAX_DATA_SIZE, 0)) == -1) {
perror(“fail to receive datas”);
exit(1);
}
printf(“Server: %s \n”, recvBuf);
memset(recvBuf, 0x00, MAX_DATA_SIZE);
}
close(sockfd);
}
/*
* Chat on Linux Terminal–alpha
* Worte by Jimmy’s team@uestc
* 2011-2-23
*
* This is the sorce code of server
* Some BUGS still unsloved, but we are trying our best to debug
* Be sure that your system’s port “1234” is not busy!
*
* */
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define SERVPORT 1234
#define BACKLOG 20
#define MAX_CON_NO 10
#define MAX_DATA_SIZE 1024
int main(int argc, char *argv[]) {
struct sockaddr_in serverSockaddr, clientSockaddr;
int sinSize, recvBytes, sendBytes;
fd_set readfd;
fd_set writefd;
int sockfd, clientfd;
char sendBuf[MAX_DATA_SIZE], recvBuf[MAX_DATA_SIZE];
if(argc != 1) {
printf(“usage:./server\n”);
exit(1);
}
/*establish a socket*/
if((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
perror(“fail to establish a socket”);
exit(1);
}
printf(“Success to establish a socket…(sockfd = %d)\n”, sockfd);
/*init sockaddr_in*/
serverSockaddr.sin_family = AF_INET;
serverSockaddr.sin_port = htons(SERVPORT);
serverSockaddr.sin_addr.s_addr = htonl(INADDR_ANY);
bzero(&(serverSockaddr.sin_zero), 8);
int on = 1;
setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
/*bind socket*/
if(bind(sockfd, (struct sockaddr *)&serverSockaddr, sizeof(struct sockaddr))== -1) {
perror(“fail to bind”);
exit(1);
}
printf(“Success to bind the socket…\n”);
/*listen on the socket*/
if(listen(sockfd, BACKLOG) == -1) {
perror(“fail to listen”);
exit(1);
}
printf(“Success to listen on the socket…\n”);
while(1) {
FD_ZERO(&readfd);
FD_SET(sockfd, &readfd);
sinSize = sizeof(struct sockaddr_in);
if(select(MAX_CON_NO, &readfd, NULL, NULL, (struct timeval *)0) > 0) {
if(FD_ISSET(sockfd, &readfd) > 0) {
/*accept a client’s request*/
if((clientfd = accept(sockfd, (struct sockaddr *)&clientSockaddr, &sinSize)) == -1) {
perror(“fail to accept”);
exit(1);
}
printf(“Success to accpet a connection request…\n”);
printf(“\033[40;32m%s join in!\033[1m\n”, inet_ntoa(clientSockaddr.sin_addr));//include color set
while(1) {
/*receive datas from client*/
if((recvBytes = recv(clientfd, recvBuf, MAX_DATA_SIZE, 0)) == -1) {
perror(“fail to receive datas”);
exit(1);
}
printf(“Client:%s\n”, recvBuf);
memset(recvBuf, 0x00, MAX_DATA_SIZE);
/*send datas to client*/
printf(“Server:”);
gets(sendBuf);
if((sendBytes = send(clientfd, sendBuf, strlen(sendBuf), 0)) != strlen(sendBuf)) {
perror(“fail to send datas”);
exit(1);
}
printf(“(Success to send data!)\n”);
memset(sendBuf, 0x00, MAX_DATA_SIZE);
}
}
close(sockfd);
}
}
}
运行方法:
jimmy@MyPet:~$ gcc -o client client.c
jimmy@MyPet:~$ gcc -o server server.c
jimmy@MyPet:~$ ./server
Success to establish a socket…(sockfd = 3)
Success to bind the socket…
Success to listen on the socket…
jimmy@MyPet:~$ ./client MyPet
Success to get host by name…
Success to establish a socket…
Success to connect the socket…
Welcome to join 127.0.1.1!
Client:
发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/146382.html原文链接:https://javaforall.cn
【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛
【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...