Сервер и клиент не видят друг друга
07.04.2018, 12:57. Показов 1184. Ответов 0
Сервер и клиент не видят друг друга. Как нужно изменить код чтобы они работали корректно? Заранее спасибо.
Вот код клиента:
C++ | 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
| #pragma comment (lib, "ws2_32.lib")
#include <Winsock2.h>
#include <iostream>
#include <string>
#include <fstream>
using namespace System;
using namespace std;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Windows::Forms;
using namespace System::Data;
using namespace System::Drawing;
using namespace System::Net;
using namespace System::IO;
#pragma once
WSAData wasData;
WORD DllVersion = MAKEWORD(2, 2);
SOCKADDR_IN addr;
SOCKET Connection = socket(AF_INET, SOCK_STREAM, NULL);
int sizeofaddr = sizeof(addr);
namespace HomeConsolleClientSockets {
using namespace System;
using namespace std;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Windows::Forms;
using namespace System::Data;
using namespace System::Drawing;
using namespace System::Net;
using namespace System::IO;
/// <summary>
/// Сводка для MyForm
/// </summary>
public ref class MyForm : public System::Windows::Forms::Form
{
public:
public:
MyForm(void)
{
InitializeComponent();
//
//TODO: добавьте код конструктора
//
}
protected:
/// <summary>
/// Освободить все используемые ресурсы.
/// </summary>
~MyForm()
{
if (components)
{
delete components;
}
}
private: System::Windows::Forms::Button^ connectButton;
protected:
private: System::Windows::Forms::TextBox^ portBox;
private: System::Windows::Forms::Label^ label2;
private: System::Windows::Forms::Label^ label3;
private: System::Windows::Forms::TextBox^ nameBox;
private: System::Windows::Forms::TextBox^ lastMessegeBox;
private: System::Windows::Forms::Label^ label4;
protected:
private:
/// <summary>
/// Обязательная переменная конструктора.
/// </summary>
System::ComponentModel::Container ^components;
#pragma region Windows Form Designer generated code
/// <summary>
/// Требуемый метод для поддержки конструктора — не изменяйте
/// содержимое этого метода с помощью редактора кода.
/// </summary>
void InitializeComponent(void)
{
this->connectButton = (gcnew System::Windows::Forms::Button());
this->portBox = (gcnew System::Windows::Forms::TextBox());
this->label2 = (gcnew System::Windows::Forms::Label());
this->label3 = (gcnew System::Windows::Forms::Label());
this->nameBox = (gcnew System::Windows::Forms::TextBox());
this->lastMessegeBox = (gcnew System::Windows::Forms::TextBox());
this->label4 = (gcnew System::Windows::Forms::Label());
this->SuspendLayout();
//
// connectButton
//
this->connectButton->Location = System::Drawing::Point(31, 72);
this->connectButton->Name = L"connectButton";
this->connectButton->Size = System::Drawing::Size(152, 23);
this->connectButton->TabIndex = 0;
this->connectButton->Text = L"Подключиться к серверу";
this->connectButton->UseVisualStyleBackColor = true;
this->connectButton->Click += gcnew System::EventHandler(this, &MyForm::connectButton_Click);
//
// portBox
//
this->portBox->Location = System::Drawing::Point(64, 44);
this->portBox->Multiline = true;
this->portBox->Name = L"portBox";
this->portBox->Size = System::Drawing::Size(118, 23);
this->portBox->TabIndex = 2;
//
// label2
//
this->label2->AutoSize = true;
this->label2->Location = System::Drawing::Point(25, 47);
this->label2->Name = L"label2";
this->label2->Size = System::Drawing::Size(37, 13);
this->label2->TabIndex = 4;
this->label2->Text = L"PORT";
//
// label3
//
this->label3->AutoSize = true;
this->label3->Location = System::Drawing::Point(5, 15);
this->label3->Name = L"label3";
this->label3->Size = System::Drawing::Size(57, 13);
this->label3->TabIndex = 5;
this->label3->Text = L"Ваше имя";
//
// nameBox
//
this->nameBox->Location = System::Drawing::Point(64, 12);
this->nameBox->Multiline = true;
this->nameBox->Name = L"nameBox";
this->nameBox->Size = System::Drawing::Size(118, 23);
this->nameBox->TabIndex = 6;
//
// lastMessegeBox
//
this->lastMessegeBox->Location = System::Drawing::Point(18, 123);
this->lastMessegeBox->Multiline = true;
this->lastMessegeBox->Name = L"lastMessegeBox";
this->lastMessegeBox->ReadOnly = true;
this->lastMessegeBox->Size = System::Drawing::Size(167, 116);
this->lastMessegeBox->TabIndex = 8;
//
// label4
//
this->label4->AutoSize = true;
this->label4->Location = System::Drawing::Point(8, 107);
this->label4->Name = L"label4";
this->label4->Size = System::Drawing::Size(182, 13);
this->label4->TabIndex = 9;
this->label4->Text = L"Последнее сообщение от сервера";
//
// MyForm
//
this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
this->BackColor = System::Drawing::SystemColors::ButtonShadow;
this->ClientSize = System::Drawing::Size(1264, 761);
this->Controls->Add(this->label4);
this->Controls->Add(this->lastMessegeBox);
this->Controls->Add(this->nameBox);
this->Controls->Add(this->label3);
this->Controls->Add(this->label2);
this->Controls->Add(this->portBox);
this->Controls->Add(this->connectButton);
this->Name = L"MyForm";
this->Text = L"MyForm";
this->ResumeLayout(false);
this->PerformLayout();
}
#pragma endregion
private: System::Void connectButton_Click(System::Object^ sender, System::EventArgs^ e) { //Кнопка "Подключиться"
addr.sin_family = AF_INET;
addr.sin_port = htons(7770);
addr.sin_addr.S_un.S_addr = INADDR_ANY;
if (connect(Connection, (SOCKADDR*)&addr, sizeofaddr) != 0)
lastMessegeBox->Text = "Failed to connect :(";
};
};
}; |
|
Вот код сервера:
C++ | 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
| #pragma comment (lib, "ws2_32.lib")
#include <Winsock2.h>
#include <iostream>
#include <WS2tcpip.h>
using namespace std;
SOCKET Connect;
SOCKET* Connections;
SOCKET Listen;
int ClientCount = 0;
void SendMessegeToClient(int ID)
{
char* buffer = new char[1024];
for (;; Sleep(75))
{
memset(buffer, 0, sizeof(buffer));
if (recv(Connections[ID], buffer, 1024, NULL))
{
printf(buffer);
printf("\n");
for (int i = 0; i <= ClientCount; i++)
{
send(Connections[i], buffer, strlen(buffer), NULL);
}
}
}
delete buffer;
}
int main()
{
setlocale(LC_ALL, "russian");
WSAData data;
WORD version = MAKEWORD(2, 2);
int res = WSAStartup(version, &data);
if (res != 0)
{
return 0;
}
struct addrinfo hints;
struct addrinfo * result;
Connections = (SOCKET*)calloc(64, sizeof(SOCKET));
ZeroMemory(&hints, sizeof(hints));
hints.ai_family = AF_INET;
hints.ai_flags = AI_PASSIVE;
hints.ai_socktype = SOCK_STREAM;
hints.ai_protocol = IPPROTO_TCP;
getaddrinfo(NULL, "7770",&hints, &result);
Listen = socket(result->ai_family, result->ai_socktype, result->ai_protocol);
bind(Listen, result->ai_addr, result->ai_addrlen);
listen(Listen, SOMAXCONN);
freeaddrinfo(result);
printf("Start server...");
char m_connect[] = "Connect...;;;5";
for (;; Sleep(75))
{
if (Connect = accept(Listen, NULL, NULL))
{
printf("Client connect...\n");
Connections[ClientCount] = Connect;
send(Connections[ClientCount], m_connect, strlen(m_connect), NULL);
ClientCount++;
CreateThread(NULL, NULL, (LPTHREAD_START_ROUTINE)SendMessegeToClient, (LPVOID)(ClientCount - 1), NULL, NULL);
}
return 1;
}
} |
|
0
|