Tcpsocket
2016-03-14 16:11
351 查看
qt服务器:
#include <QCoreApplication>
#include <stdlib.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <unistd.h>
#include <QDebug>
#define PORT 3880
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
int listenfd;
int confd;
int n;
char buf[100];
socklen_t clientInfo; // socket len
struct sockaddr_in cliaddr,serverAddr;
qDebug()<<"创建socket套接字"<<endl;
listenfd = socket(AF_INET,SOCK_STREAM,0);
serverAddr.sin_family = AF_INET;
serverAddr.sin_addr.s_addr= htonl(INADDR_ANY);
serverAddr.sin_port=htons(PORT);
qDebug()<<" connection。。。。"<<endl;
bind(listenfd,(struct sockaddr*)&serverAddr,sizeof(serverAddr));
listen(listenfd,8);
while(true)
{
clientInfo = sizeof(cliaddr);
confd = accept(listenfd,(struct sockaddr*)&cliaddr,&clientInfo);
if ((n = recv(confd,buf,100,0))>0)
{
qDebug()<<"recevie data is:"<<buf<<endl;
}
qDebug()<<"socket information:"<<confd<<endl;
qDebug()<<"client ip:"<<cliaddr.sin_addr.s_addr<<endl;
if(!fork())
{
close(listenfd);
while((n = recv(confd,buf,100,0))>0)
{
puts(buf);
}
}
// close(confd);
// exit(0);
}
close(listenfd);
return a.exec();
}
ios客户端:
#import "ViewController.h"
#include <sys/socket.h>
#include <netinet/in.h>
#include <unistd.h>
#include <arpa/inet.h>
#define PORT 3880;
@interface
ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super
viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning {
[super
didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(IBAction)click:(id)sender
{
NSLog(@"connection server");
struct sockaddr_in seraddr;
char sendLine[100] =
"HELLO WORLD";
int sockfd = socket(AF_INET,
SOCK_STREAM, 0);
if(sockfd <0)
{
int temp =
errno;
NSLog(@"%d",errno);
}
memset(&seraddr,
0, sizeof(seraddr));
seraddr.sin_family =
AF_INET;
seraddr.sin_addr.s_addr =
inet_addr("192.168.3.166");
seraddr.sin_port =
htons(3880);
int fd;
int conInfo =
connect(sockfd, (struct
sockaddr*)&seraddr, sizeof(seraddr));
if(conInfo<0)
NSLog(@"connection information error: ");
// while (fgets(sendLine, 100, stdin) != NULL) {
// send(fd,sendLine,strlen(sendLine),0);
// }
send(fd,sendLine,strlen(sendLine),0);
}
@end
#include <QCoreApplication>
#include <stdlib.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <unistd.h>
#include <QDebug>
#define PORT 3880
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
int listenfd;
int confd;
int n;
char buf[100];
socklen_t clientInfo; // socket len
struct sockaddr_in cliaddr,serverAddr;
qDebug()<<"创建socket套接字"<<endl;
listenfd = socket(AF_INET,SOCK_STREAM,0);
serverAddr.sin_family = AF_INET;
serverAddr.sin_addr.s_addr= htonl(INADDR_ANY);
serverAddr.sin_port=htons(PORT);
qDebug()<<" connection。。。。"<<endl;
bind(listenfd,(struct sockaddr*)&serverAddr,sizeof(serverAddr));
listen(listenfd,8);
while(true)
{
clientInfo = sizeof(cliaddr);
confd = accept(listenfd,(struct sockaddr*)&cliaddr,&clientInfo);
if ((n = recv(confd,buf,100,0))>0)
{
qDebug()<<"recevie data is:"<<buf<<endl;
}
qDebug()<<"socket information:"<<confd<<endl;
qDebug()<<"client ip:"<<cliaddr.sin_addr.s_addr<<endl;
if(!fork())
{
close(listenfd);
while((n = recv(confd,buf,100,0))>0)
{
puts(buf);
}
}
// close(confd);
// exit(0);
}
close(listenfd);
return a.exec();
}
ios客户端:
#import "ViewController.h"
#include <sys/socket.h>
#include <netinet/in.h>
#include <unistd.h>
#include <arpa/inet.h>
#define PORT 3880;
@interface
ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super
viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning {
[super
didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(IBAction)click:(id)sender
{
NSLog(@"connection server");
struct sockaddr_in seraddr;
char sendLine[100] =
"HELLO WORLD";
int sockfd = socket(AF_INET,
SOCK_STREAM, 0);
if(sockfd <0)
{
int temp =
errno;
NSLog(@"%d",errno);
}
memset(&seraddr,
0, sizeof(seraddr));
seraddr.sin_family =
AF_INET;
seraddr.sin_addr.s_addr =
inet_addr("192.168.3.166");
seraddr.sin_port =
htons(3880);
int fd;
int conInfo =
connect(sockfd, (struct
sockaddr*)&seraddr, sizeof(seraddr));
if(conInfo<0)
NSLog(@"connection information error: ");
// while (fgets(sendLine, 100, stdin) != NULL) {
// send(fd,sendLine,strlen(sendLine),0);
// }
send(fd,sendLine,strlen(sendLine),0);
}
@end
相关文章推荐
- Android网络问题
- https
- loadrunner简单使用——HTTP,WebService,Socket压力测试脚本编写
- iOS-网络通信协议
- Android HttpUrlconnection使用总结
- php5apache2_2.dll被appaceh httpd.conf加载出错 The requested operation has failed
- Android 第五天重置版_网络编程2_注意事项
- http_load的安装及使用方法
- tcpdump抓包二进制tcp协议详细分析
- http post get请求
- HTTP视频知识填充
- http Json 技术
- 生产环境常见的HTTP状态码列表
- 用802.1X+FreeRadius+LDAP实现网络准入方案
- iOS 基础知识大全之网络篇(可供零基础学习)
- TCP的三次握手,四次断开
- Android本地图片或者网络图片高斯模糊效果(毛玻璃效果)图片模糊效果一行代码搞定
- 配置网络策略中的 NAP 条件
- java获得磁盘、网络实时I/O速率
- iis 发布MVC HTTP错误 403.14