Linux Socket通信 C/S模型
代码片段(8)
[代码] MySocket.h
01
|
#ifndef _MYSOCKET_0623_H
|
02
|
#define _MYSOCKET_0623_H
|
03
|
04
|
#include <sys/socket.h>
|
05
|
#include <sys/types.h>
|
06
|
#include <arpa/inet.h>
|
07
|
#include <netinet/in.h>
|
08
|
#include <sys/wait.h>
|
09
|
#include <unistd.h>
|
10
|
#include <iostream>
|
11
|
#include <string.h>
|
12
|
#include <stdlib.h>
|
13
|
14
|
//Base class Socket
|
15
|
class Socket
|
16
|
{
|
17
|
public :
|
18
|
Socket();
|
19
|
virtual ~Socket();
|
20
|
bool Create();
|
21
|
bool Bind( const unsigned short port);
|
22
|
bool Listen() const ;
|
23
|
bool Accept(Socket &new_sock) const ;
|
24
|
bool Connect(std::string host, const unsigned short port); //client
|
25
|
bool Send( const std::string s) const ;
|
26
|
int Recv(std::string &s) const ;
|
27
|
void Close();
|
28
|
bool IsValid() const ;
|
29
|
30
|
private :
|
31
|
int m_sock;
|
32
|
sockaddr_in m_addr;
|
33
|
};
|
34
|
35
|
//ServerSocket, Use Socket as a service
|
36
|
class ServerSocket : private Socket
|
37
|
{
|
38
|
public :
|
39
|
ServerSocket(){Create();} //for normal data-exchange socket
|
40
|
ServerSocket(unsigned short port); //for listen socket
|
41
|
virtual ~ServerSocket();
|
42
|
void Accept(ServerSocket &new_sock) const ;
|
43
|
void Close();
|
44
|
const ServerSocket& operator << ( const std::string&) const ;
|
45
|
const ServerSocket& operator >> (std::string&) const ;
|
46
|
};
|
47
|
48
|
//ClientSocket derived form Socket
|
49
|
class ClientSocket : private Socket
|
50
|
{
|
51
|
public :
|
52
|
ClientSocket(){Create();}
|
53
|
ClientSocket(std::string host, const unsigned short port);
|
54
|
virtual ~ClientSocket();
|
55
|
void Close();
|
56
|
const ClientSocket& operator << ( const std::string&) const ;
|
57
|
const ClientSocket& operator >> (std::string&) const ;
|
58
|
};
|
59
|
60
|
//SocketException
|
61
|
class SocketException
|
62
|
{
|
63
|
public :
|
64
|
SocketException(std::string msg = std::string( "Socket Exception" )): m_msg(msg){}
|
65
|
void Print(){std::cout << m_msg << std::endl;}
|
66
|
private :
|
67
|
const std::string m_msg;
|
68
|
|
69
|
};
|
70
|
#endif
|
[代码] MySocket.cpp
001
|
#include "MySocket.h"
|
002
|
#include <assert.h>
|
003
|
using namespace std;
|
004
|
//Implement of Socket class
|
005
|
006
|
static void Trace( const string& msg = "Socket Class Error" )
|
007
|
{
|
008
|
#ifdef DEBUG
|
009
|
cout << msg << endl;
|
010
|
#endif
|
011
|
}
|
012
|
013
|
Socket::Socket()
|
014
|
{
|
015
|
m_sock = -1;
|
016
|
m_addr.sin_family = AF_INET;
|
017
|
m_addr.sin_addr.s_addr = inet_addr( "127.0.0.1" );
|
018
|
m_addr.sin_port = htons(8089);
|
019
|
}
|
020
|
021
|
Socket::~Socket()
|
022
|
{
|
023
|
if (m_sock != -1) //valid socket
|
024
|
{
|
025
|
Close();
|
026
|
}
|
027
|
}
|
028
|
029
|
bool Socket::Create()
|
030
|
{
|
031
|
m_sock = socket(AF_INET, SOCK_STREAM, 0);
|
032
|
if (m_sock == -1)
|
033
|
{
|
034
|
Trace( "Create Socket Failure" );
|
035
|
return false ;
|
036
|
}
|
037
|
else
|
038
|
{
|
039
|
Trace( "Create Socket Succeed" );
|
040
|
return true ;
|
041
|
}
|
042
|
}
|
043
|
044
|
bool Socket::Bind( const unsigned short port)
|
045
|
{
|
046
|
assert (m_sock != -1);
|
047
|
m_addr.sin_family = AF_INET;
|
048
|
m_addr.sin_addr.s_addr = inet_addr( "127.0.0.1" );
|
049
|
m_addr.sin_port = htons(port);
|
050
|
int server_len = sizeof ( m_addr );
|
051
|
if ( bind(m_sock, ( struct sockaddr *)& m_addr, server_len) == -1 )
|
052
|
{
|
053
|
Trace( "Server Bind Failure" );
|
054
|
return false ;
|
055
|
}
|
056
|
else
|
057
|
{
|
058
|
return true ;
|
059
|
}
|
060
|
}
|
061
|
062
|
bool Socket::Listen() const
|
063
|
{
|
064
|
if ( listen(m_sock, 5) == -1 )
|
065
|
{
|
066
|
Trace( "Server Listen Failure" );
|
067
|
return false ;
|
068
|
}
|
069
|
else
|
070
|
{
|
071
|
return true ;
|
072
|
}
|
073
|
}
|
074
|
075
|
bool Socket::Accept(Socket & new_socket) const //new_socket as a return
|
076
|
{
|
077
|
int len = sizeof ( new_socket.m_addr );
|
078
|
new_socket.m_sock = accept( m_sock,
|
079
|
( struct sockaddr *)& new_socket.m_addr, (socklen_t*)&len );
|
080
|
if ( new_socket.m_sock == -1 )
|
081
|
{
|
082
|
Trace( "Server Accept Failure" );
|
083
|
return false ;
|
084
|
}
|
085
|
else
|
086
|
{
|
087
|
return true ;
|
088
|
}
|
089
|
}
|
090
|
091
|
bool Socket::Connect( const std::string host, const unsigned short port)
|
092
|
{
|
093
|
assert (m_sock != -1);
|
094
|
m_addr.sin_family = AF_INET;
|
095
|
m_addr.sin_addr.s_addr = inet_addr(host.c_str());
|
096
|
m_addr.sin_port = htons(port);
|
097
|
int len = sizeof (m_addr);
|
098
|
int res = connect( m_sock, ( struct sockaddr*)& m_addr, len);
|
099
|
if (res == -1)
|
100
|
{
|
101
|
Trace( "Client Connect Failure" );
|
102
|
return false ;
|
103
|
}
|
104
|
return true ;
|
105
|
}
|
106
|
107
|
bool Socket::Send( const std::string send_str) const
|
108
|
{
|
109
|
//copy the send_str to a buffer so that we can send it.
|
110
|
size_t len = send_str.size() + 1;
|
111
|
char *send_buf = NULL;
|
112
|
send_buf = new char [len];
|
113
|
if (NULL == send_buf)
|
114
|
{
|
115
|
Trace( "Socket: memory allocation failed in Send()" );
|
116
|
return false ;
|
117
|
}
|
118
|
char t = send_str[0];
|
119
|
int i = 0;
|
120
|
while ( t != 0 )
|
121
|
{
|
122
|
send_buf[i] = t;
|
123
|
i++;
|
124
|
t = send_str[i];
|
125
|
}
|
126
|
send_buf[i] = 0;
|
127
|
//end of copying string
|
128
|
assert (i == len-1);
|
129
|
130
|
int xoff = 0;
|
131
|
int xs = 0;
|
132
|
do
|
133
|
{
|
134
|
xs = write(m_sock, send_buf+xoff, len-xoff);
|
135
|
if (-1 == xs)
|
136
|
{
|
137
|
Trace( "Socket: send data failed" );
|
138
|
delete [] send_buf;
|
139
|
return false ;
|
140
|
}
|
141
|
else
|
142
|
{
|
143
|
xoff += xs;
|
144
|
}
|
145
|
} while (xoff < len);
|
146
|
Trace( "Socket: send data succeed" );
|
147
|
delete [] send_buf;
|
148
|
return true ;
|
149
|
}
|
150
|
151
|
int Socket::Recv( std::string & recv_str) const
|
152
|
{
|
153
|
cout << "enter recv" << endl;
|
154
|
char *recv_buf;
|
155
|
recv_buf = new char [256];
|
156
|
memset (recv_buf, 0 ,256);
|
157
|
int len = read(m_sock, recv_buf, 256);
|
158
|
if (-1 == len)
|
159
|
{
|
160
|
Trace( "Socket: recv data failed" );
|
161
|
delete [] recv_buf;
|
162
|
return -1;
|
163
|
}
|
164
|
else
|
165
|
{
|
166
|
recv_str = recv_buf;
|
167
|
delete [] recv_buf;
|
168
|
return len;
|
169
|
}
|
170
|
}
|
171
|
172
|
173
|
bool Socket::IsValid() const
|
174
|
{
|
175
|
return ( m_sock != -1 );
|
176
|
}
|
177
|
178
|
void Socket::Close()
|
179
|
{
|
180
|
if (m_sock != -1)
|
181
|
{
|
182
|
close(m_sock);
|
183
|
m_sock = -1;
|
184
|
}
|
185
|
|
186
|
}
|
187
|
//
|
[代码] MyClientSocket.cpp
01
|
#include "MySocket.h"
|
02
|
using namespace std;
|
03
|
04
|
static void Trace( const string& msg = "ServerSocket Class Error" )
|
05
|
{
|
06
|
#ifdef DEBUG
|
07
|
cout << msg << endl;
|
08
|
#endif
|
09
|
}
|
10
|
11
|
ClientSocket::ClientSocket(std::string host, const unsigned short port)
|
12
|
{
|
13
|
if ( !Create() )
|
14
|
{
|
15
|
Trace( "Client Socket Constructor Failed" );
|
16
|
throw SocketException( "Client Socket Constructor Failed" );
|
17
|
}
|
18
|
if ( !Connect(host, port) )
|
19
|
{
|
20
|
throw SocketException( "Client Socket Constructor Failed" );
|
21
|
}
|
22
|
}
|
23
|
24
|
ClientSocket::~ClientSocket()
|
25
|
{
|
26
|
Close();
|
27
|
}
|
28
|
29
|
void ClientSocket::Close()
|
30
|
{
|
31
|
Socket::Close();
|
32
|
}
|
33
|
34
|
const ClientSocket& ClientSocket::operator << ( const std::string &s) const
|
35
|
{
|
36
|
if ( !Send(s) )
|
37
|
{
|
38
|
Trace( "Could not write to server socket" );
|
39
|
throw SocketException( "Could not write to client socket" );
|
40
|
}
|
41
|
return * this ;
|
42
|
}
|
43
|
44
|
const ClientSocket& ClientSocket::operator >> (std::string& s) const
|
45
|
{
|
46
|
if ( Recv(s) == -1 )
|
47
|
{
|
48
|
Trace( "Could not read from socket" );
|
49
|
throw SocketException( "Could not read from client socket" );
|
50
|
}
|
51
|
return * this ;
|
52
|
}
|
[代码] MyServerSocket.cpp
01
|
#include "MySocket.h"
|
02
|
#include <assert.h>
|
03
|
using namespace std;
|
04
|
05
|
static void Trace( const string& msg = "ServerSocket Class Error" )
|
06
|
{
|
07
|
#ifdef DEBUG
|
08
|
cout << msg << endl;
|
09
|
#endif
|
10
|
}
|
11
|
12
|
ServerSocket::ServerSocket(unsigned short port)
|
13
|
{
|
14
|
if ( !Create() )
|
15
|
{
|
16
|
Trace( "Could not create server socket!" );
|
17
|
throw SocketException( "Could not create : server socket!" );
|
18
|
}
|
19
|
if ( !Bind(port) )
|
20
|
{
|
21
|
Trace( "Could not bind to port!" );
|
22
|
throw SocketException( "Could not Bind : server socket!" );
|
23
|
}
|
24
|
if ( !Listen() )
|
25
|
{
|
26
|
Trace( "Could not listen to socket" );
|
27
|
throw SocketException( "Could not Litsen : server socket!" );
|
28
|
}
|
29
|
}
|
30
|
31
|
ServerSocket::~ServerSocket()
|
32
|
{
|
33
|
Close();
|
34
|
}
|
35
|
36
|
37
|
void ServerSocket::Accept(ServerSocket& new_socket) const
|
38
|
{
|
39
|
if ( !Socket::Accept(new_socket) )
|
40
|
{
|
41
|
throw SocketException( "Server Accept Failed" );
|
42
|
}
|
43
|
}
|
44
|
45
|
void ServerSocket::Close()
|
46
|
{
|
47
|
Socket::Close();
|
48
|
}
|
49
|
50
|
const ServerSocket& ServerSocket::operator << ( const std::string& s) const
|
51
|
{
|
52
|
if ( !Send(s) )
|
53
|
{
|
54
|
Trace( "Could not write to server socket" );
|
55
|
throw SocketException( "Could not write to server socket" );
|
56
|
}
|
57
|
return * this ;
|
58
|
}
|
59
|
60
|
const ServerSocket& ServerSocket::operator >> (std::string& s) const
|
61
|
{
|
62
|
int ret = Recv(s);
|
63
|
if ( ret == -1 )
|
64
|
{
|
65
|
Trace( "Could not read from server socket" );
|
66
|
throw SocketException( "Could not read form server socket" );
|
67
|
}
|
68
|
return * this ;
|
69
|
}
|
[代码] ServerTest.cpp
01
|
//Create a server for testing
|
02
|
#include "MySocket.h"
|
03
|
#include "Calculator.h"
|
04
|
using namespace std;
|
05
|
int main()
|
06
|
{
|
07
|
try
|
08
|
{
|
09
|
int result;
|
10
|
ServerSocket server(30000);
|
11
|
|
12
|
string s;
|
13
|
while ( true )
|
14
|
{
|
15
|
ServerSocket new_sock;
|
16
|
server.Accept( new_sock );
|
17
|
while ( true )
|
18
|
{
|
19
|
new_sock >> s;
|
20
|
cout << "receive : \t" << s << endl;
|
21
|
//Deal with client requests
|
22
|
new_sock << s;
|
23
|
cout << "send: \t " << s << endl;
|
24
|
}
|
25
|
new_sock.Close();
|
26
|
}
|
27
|
|
28
|
}
|
29
|
catch (SocketException &e)
|
30
|
{
|
31
|
cout << "SocketException:\t" ;
|
32
|
e.Print();
|
33
|
}
|
34
|
catch (...)
|
35
|
{
|
36
|
cout << "Common Exception" << endl;
|
37
|
}
|
38
|
return 0;
|
39
|
}
|
[代码] ClientTest.cpp
01
|
#include "MySocket.h"
|
02
|
#include "Calculator.h"
|
03
|
using namespace std;
|
04
|
05
|
int main()
|
06
|
{
|
07
|
try
|
08
|
{
|
09
|
ClientSocket client( "127.0.0.1" ,30000);
|
10
|
string s = "HELLO" ;
|
11
|
|
12
|
int count = 1;
|
13
|
while (count <= 5)
|
14
|
{
|
15
|
cout << "input a string : " ;
|
16
|
cin >> s;
|
17
|
cout << "You have input " << s << endl;
|
18
|
client << s;
|
19
|
cout<< "Send to Socket : " <<s<< '\t' <<count<<endl;
|
20
|
client >> s;
|
21
|
cout<< "Read from Socket : " <<s<< '\t' <<count<<endl;
|
22
|
//sleep(2);
|
23
|
s = "HELLO" ;
|
24
|
count++;
|
25
|
}
|
26
|
client.Close();
|
27
|
}
|
28
|
catch (SocketException &e)
|
29
|
{
|
30
|
cout << "SocketException:\t" ;
|
31
|
e.Print();
|
32
|
}
|
33
|
///
|
34
|
|
35
|
return 0;
|
36
|
}
|
[代码] MakeClient
01
|
objects = ClientTest.o MyClientSocket.o MySocket.o Calculator.o mathop.o
|
02
|
ClientTest.out: $(objects)
|
03
|
g++ $(objects) -o ClientTest.out
|
04
|
ClientTest.o: ClientTest.cpp MySocket.h Calculator.h
|
05
|
g++ -c -g -Wall -pedantic -ansi -DDEBUG -o ClientTest.o ClientTest.cpp
|
06
|
MyClientSocket.o: MyClientSocket.cpp MySocket.h
|
07
|
g++ -c -g -Wall -pedantic -ansi -DDEBUG -o MyClientSocket.o MyClientSocket.cpp
|
08
|
MySocket.o: MySocket.cpp MySocket.h
|
09
|
g++ -c -g -Wall -pedantic -ansi -DDEBUG MySocket.cpp -o MySocket.o
|
10
|
Calculator.o: Calculator.cpp Calculator.h
|
11
|
g++ -g -c -Wall -pedantic -ansi -DDEBUG -o Calculator.o Calculator.cpp
|
12
|
mathop.o: mathop.cpp mathop.h
|
13
|
g++ -g -c -Wall -pedantic -ansi -DDEBUG -o mathop.o mathop.cpp
|
14
|
.PHONY: clean
|
15
|
clean:
|
16
|
-rm $(objects) ClientTest.out
|
17
|
|
[代码] MakeServer
01
|
objects = ServerTest.o MyServerSocket.o MySocket.o Calculator.o mathop.o
|
02
|
ServerTest.out: $(objects)
|
03
|
g++ $(objects) -o ServerTest.out
|
04
|
ServerTest.o: ServerTest.cpp MySocket.h Calculator.h
|
05
|
g++ -c -g -Wall -pedantic -ansi -DDEBUG -o ServerTest.o ServerTest.cpp
|
06
|
MyServerSocket.o: MyServerSocket.cpp MySocket.h
|
07
|
g++ -c -g -Wall -pedantic -ansi -DDEBUG -o MyServerSocket.o MyServerSocket.cpp
|
08
|
MySocket.o: MySocket.cpp MySocket.h
|
09
|
g++ -c -g -Wall -pedantic -ansi -DDEBUG MySocket.cpp -o MySocket.o
|
10
|
Calculator.o: Calculator.cpp Calculator.h
|
11
|
g++ -g -c -Wall -pedantic -ansi -DDEBUG -o Calculator.o Calculator.cpp
|
12
|
mathop.o: mathop.cpp mathop.h
|
13
|
g++ -g -c -Wall -pedantic -ansi -DDEBUG -o mathop.o mathop.cpp
|
14
|
.PHONY: clean
|
15
|
clean:
|
16
|
-rm $(objects) ServerTest.out
|
17
|
|
Linux Socket通信 C/S模型相关推荐
- linux socket通信出错封装处理
linux socket通信出错封装处理 wrap.c #include <stdlib.h> #include <errno.h> #include <sys/sock ...
- socket 通信之 epoll模型
//socket 通信之 epoll 模型 #include <sys/epoll.h>struct epoll_event{uint32_t events;//需要检测的fd事件,取值与 ...
- linux socket通信组件,Linux下socket简单通信
#define MAXLINE 4096 /**************程序流程******************* 1 用socket()函数创建一个socket 2 用bind()绑定到一个本地 ...
- linux socket通信编程之c语言(客户端和服务器程序)
最近在学习socket编程,在网上找到一个很好的实例,于是按自己的想把代码修改了一下,算半原创了~~~~ 要保存起来常复习呀~~~ /// //服务器端代码:server.c /// //本文件是服务 ...
- linux socket通信tcp,基于TCP协议的socket通信
一.服务器端 1.创建serverSocket,即服务器端的socket,绑定指定的端口,并侦听此端口 ServerSocket server = new ServerSocket(8888); 2. ...
- socket通信和异常处理札记
Linux socket通信出现CLOSE_WAIT状态的原因与解决方法 这个问题之前没有怎么留意过,是最近在面试过程中遇到的一个问题,面了两家公司,两家公司竟然都面到到了这个问题,不得不使我开始关注 ...
- linux系统udp通信程序,Linux UDP socket编程(UDP通讯模型) | C/C++程序员之家
Linux UDP socket编程(UDP通讯模型): UDPClient + UDPService. Linux下大多数网络程序都是基于TCP的,很少基于UDP,简单的通讯模型如下,开发时候备用! ...
- Linux系统下实现基于TCP/IP协议的简单Socket通信
Linux系统下实现基于TCP/IP协议的简单Socket通信 网络套接字Socket Socket概念 主机字节序和网络字节序 Sockaddr地址结构 Socket实现客户端.服务端通信 服务端程 ...
- Linux C++服务器项目——网络编程1 (socket通信,服务端,客户端)
牛客 C++高并发服务器开发 参考笔记 1.MAC地址 2 IP地址 2.1 简介 2.2 IP地址编址方式 2.3 子网掩码 3 端口 3.1 简介 3.2 端口类型 4 网络模型 4.1 OSI七 ...
最新文章
- 正则表达式获取TABLE里的内容
- 马哥教育第二十二天IO模型理论、数据库基础理论、LAMP平台理论基础及mysql部署...
- Python—实训day10—Matplotlib数据可视化和scikit-learn构建模型
- 亚麻纤维截面形态_天然丝纤维蚕丝
- 【Java】Java数据库访问体系重点总结
- [LeetCode] Power of Four
- JVM垃圾收集器(2)
- 免登录实现CSDN博客代码复制(2021.9.26)
- 佳博LabelShop模板文件转成佳博打印机指令软件
- PostgreSQL 中文手册
- 温故知新(七七)nextTick 是在本次循环执行,还是在下次,setTimeout(()=>{},0)呢
- 卓文萱在北京净万家像街头卖艺似的骗子粉丝做公益绯闻男友辰亦儒看不惯假惺惺模样破口大骂
- [35期] 没有硝烟的战争
- 什么是图形加速卡(二)
- POJ-1436___Horizontally Visible Segments —— 线段树
- 【笔记】用函数实现折半查找
- cesium面积测量
- springboot使用ResourceUtils.getFile读取不到文件
- 可参考的oa系统demo,抠图网站,机票界面,博客开发网站
- 小程序 globalData 详解
热门文章
- A20 文件系统预装APK
- WINCE6开机进度条
- WinCE驱动调试助手V2.5
- 判断深度学习模型的稳定性_问题引领构建数学模型,讲练结合促进深度学习
- 【转】注册Azure AD 应用程序
- c语言3种链接属性: 外部(external), 内部(internal),无设置(none)
- c语言哪个方法称为程序大门,学会这8个经典小程序,就相当于跨入了C语言大门...
- Qt应用程序发布:Qt应用程序添加版本版权生产商等信息
- Qt下OpenCv中cvGetWindowHandle的使用
- c语言 有趣的代码,分享一段有趣的小代码