tcp 聊天工具客户端
来源:互联网 发布:海关数据是什么 编辑:程序博客网 时间:2024/05/21 17:09
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <sys/socket.h>
#include <resolv.h>
#include <stdlib.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#define MAXBUF 1024
int main(int argc, char *argv[])
{
int sockfd,len;
struct sockaddr_in dest;
char buffer[MAXBUF+1];
if ( argc != 3 )
{
printf("error format, it must be \n\t\t %s ip port \n",argv[0]);
exit(EXIT_FAILURE);
}
if ( (sockfd = socket(AF_INET,SOCK_STREAM,0)) <0)
{
printf("socket\n");
exit(errno);
}
printf("socket created \n");
bzero(&dest, sizeof(dest));
dest.sin_family = AF_INET;
dest.sin_port = htons(atoi(argv[2]));
if ( inet_aton(argv[1], (struct in_addr*)&dest.sin_addr.s_addr) == 0)
{
perror(argv[1]);
exit(errno);
}
if ( connect(sockfd, (struct sockaddr *)&dest, sizeof(dest)) == -1 )
{
perror("connect");
exit(errno);
}
printf("server connected \n");
while ( 1 )
{
bzero(buffer, MAXBUF+1);
len = recv(sockfd, buffer, MAXBUF, 0);
if ( len > 0 )
{
printf("recv successful :'%s',%d byte recv \n",buffer, len);
}
else
{
if ( len<0 )
{
printf("send failure ,errno code is %d, err msg is '%s'\n",errno, strerror(errno));
}
else
{
printf("the other one close, quit \n");
break;
}
}
bzero(buffer, MAXBUF+1);
printf("pls send msg to send :")
fgets(buffer, MAXBUF, stdin);
if ( !strncasecmp(buffer, "quit", 4) )
{
printf("I WILL QUIT \n");
break;
}
len = send(sockfd, buffer, strlen(buffer)-1,0);
if ( len < 0 )
{
printf("message '%s' send failure, errno code is %d, errno msg is '%s' \n",buffer,errno,strerror(errno));
break;
}
else
{
printf("message is %s \t send successful, %d byte send!\n",buffer,len);
}
close(sockfd);
return 0;
}
}
#include <string.h>
#include <errno.h>
#include <sys/socket.h>
#include <resolv.h>
#include <stdlib.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#define MAXBUF 1024
int main(int argc, char *argv[])
{
int sockfd,len;
struct sockaddr_in dest;
char buffer[MAXBUF+1];
if ( argc != 3 )
{
printf("error format, it must be \n\t\t %s ip port \n",argv[0]);
exit(EXIT_FAILURE);
}
if ( (sockfd = socket(AF_INET,SOCK_STREAM,0)) <0)
{
printf("socket\n");
exit(errno);
}
printf("socket created \n");
bzero(&dest, sizeof(dest));
dest.sin_family = AF_INET;
dest.sin_port = htons(atoi(argv[2]));
if ( inet_aton(argv[1], (struct in_addr*)&dest.sin_addr.s_addr) == 0)
{
perror(argv[1]);
exit(errno);
}
if ( connect(sockfd, (struct sockaddr *)&dest, sizeof(dest)) == -1 )
{
perror("connect");
exit(errno);
}
printf("server connected \n");
while ( 1 )
{
bzero(buffer, MAXBUF+1);
len = recv(sockfd, buffer, MAXBUF, 0);
if ( len > 0 )
{
printf("recv successful :'%s',%d byte recv \n",buffer, len);
}
else
{
if ( len<0 )
{
printf("send failure ,errno code is %d, err msg is '%s'\n",errno, strerror(errno));
}
else
{
printf("the other one close, quit \n");
break;
}
}
bzero(buffer, MAXBUF+1);
printf("pls send msg to send :")
fgets(buffer, MAXBUF, stdin);
if ( !strncasecmp(buffer, "quit", 4) )
{
printf("I WILL QUIT \n");
break;
}
len = send(sockfd, buffer, strlen(buffer)-1,0);
if ( len < 0 )
{
printf("message '%s' send failure, errno code is %d, errno msg is '%s' \n",buffer,errno,strerror(errno));
break;
}
else
{
printf("message is %s \t send successful, %d byte send!\n",buffer,len);
}
close(sockfd);
return 0;
}
}
0 0
- tcp 聊天工具客户端
- 聊天工具 客户端
- JAVA进阶案例 TCP编程之网络聊天工具(客户端)
- 多线程控制台TCP聊天工具
- 利用TCP协议通过Socket编写的网络聊天工具1-客户端
- 基于TCP的QQ聊天工具
- java 简易聊天工具(TCP)
- 基于TCP的多人聊天工具
- 自编JAVA聊天工具(客户端)
- 配置openfire服务器+spark客户端IM聊天工具
- 聊天工具
- 聊天工具
- TCP客户端
- tcp客户端
- TCP客户端
- TCP客户端
- TCP客户端
- TCP 客户端
- Search in Rotated Sorted Array II
- LightOJ 1356 Prime Independence (素数 二分图)
- Search in Rotated Sorted Array
- (一) Vim使用心得一
- hdu 1711 Number Sequence KMP模板题
- tcp 聊天工具客户端
- Python开发神器-pycharm3.4.1注册版下载
- Maven实战——常用Maven插件介绍
- Java学习总结(一)
- 在Markdown中使用中文
- CentOS(5.8/6.4)linux生产环境若干优化实战
- I/O流操作规律
- 位操作之求绝对值
- 聊天工具服务器