Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
P
Performance von Anwendungen
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Saif Eddine Askri
Performance von Anwendungen
Commits
26c9be18
Commit
26c9be18
authored
2 months ago
by
Saif Eddine Askri
Browse files
Options
Downloads
Patches
Plain Diff
add server and client
parent
ebfed5f8
No related branches found
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
prak3/src/client.cpp
+53
-0
53 additions, 0 deletions
prak3/src/client.cpp
prak3/src/server.cpp
+81
-0
81 additions, 0 deletions
prak3/src/server.cpp
with
134 additions
and
0 deletions
prak3/src/client.cpp
+
53
−
0
View file @
26c9be18
#include
<iostream>
#include
<cstring>
#include
<unistd.h>
#include
<arpa/inet.h>
#define SERVER_IP "127.0.0.1"
#define PORT 8080
#define BUFFER_SIZE 1024
int
main
()
{
int
sock
;
struct
sockaddr_in
server_address
;
char
buffer
[
BUFFER_SIZE
]
=
{
0
};
// Socket erstellen
sock
=
socket
(
AF_INET
,
SOCK_STREAM
,
0
);
if
(
sock
==
-
1
)
{
perror
(
"Socket failed"
);
exit
(
EXIT_FAILURE
);
}
// Serveradresse konfigurieren
server_address
.
sin_family
=
AF_INET
;
server_address
.
sin_port
=
htons
(
PORT
);
// IP-Adresse konvertieren
if
(
inet_pton
(
AF_INET
,
SERVER_IP
,
&
server_address
.
sin_addr
)
<=
0
)
{
perror
(
"Invalid address"
);
exit
(
EXIT_FAILURE
);
}
// Verbindung zum Server herstellen
if
(
connect
(
sock
,
(
struct
sockaddr
*
)
&
server_address
,
sizeof
(
server_address
))
<
0
)
{
perror
(
"Connection failed"
);
exit
(
EXIT_FAILURE
);
}
// Nachricht senden
const
char
*
message
=
"Hello"
;
send
(
sock
,
message
,
strlen
(
message
),
0
);
std
::
cout
<<
"Message sent: "
<<
message
<<
std
::
endl
;
// Antwort empfangen
ssize_t
bytes_received
=
read
(
sock
,
buffer
,
BUFFER_SIZE
);
if
(
bytes_received
>
0
)
{
std
::
cout
<<
"Server: "
<<
buffer
<<
std
::
endl
;
}
// Socket schließen
close
(
sock
);
return
0
;
}
This diff is collapsed.
Click to expand it.
prak3/src/server.cpp
+
81
−
0
View file @
26c9be18
#include
<iostream>
#include
<thread>
#include
<vector>
#include
<cstring>
#include
<unistd.h>
#include
<arpa/inet.h>
#define PORT 8080
#define BUFFER_SIZE 1024
void
handle_client
(
int
client_socket
)
{
char
buffer
[
BUFFER_SIZE
]
=
{
0
};
// Nachricht vom Client empfangen
ssize_t
bytes_received
=
read
(
client_socket
,
buffer
,
BUFFER_SIZE
);
if
(
bytes_received
>
0
)
{
std
::
cout
<<
"Client: "
<<
buffer
<<
std
::
endl
;
// Antwort an den Client senden
const
char
*
response
=
"Hi"
;
send
(
client_socket
,
response
,
strlen
(
response
),
0
);
}
// Socket schließen
close
(
client_socket
);
}
int
main
()
{
int
server_fd
,
client_socket
;
struct
sockaddr_in
address
;
socklen_t
addrlen
=
sizeof
(
address
);
std
::
vector
<
std
::
thread
>
threads
;
// Socket erstellen
server_fd
=
socket
(
AF_INET
,
SOCK_STREAM
,
0
);
if
(
server_fd
==
-
1
)
{
perror
(
"Socket failed"
);
exit
(
EXIT_FAILURE
);
}
// Serveradresse festlegen
address
.
sin_family
=
AF_INET
;
address
.
sin_addr
.
s_addr
=
INADDR_ANY
;
address
.
sin_port
=
htons
(
PORT
);
// Socket an Port binden
if
(
bind
(
server_fd
,
(
struct
sockaddr
*
)
&
address
,
sizeof
(
address
))
<
0
)
{
perror
(
"Bind failed"
);
exit
(
EXIT_FAILURE
);
}
// Server lauscht auf eingehende Verbindungen
if
(
listen
(
server_fd
,
5
)
<
0
)
{
perror
(
"Listen failed"
);
exit
(
EXIT_FAILURE
);
}
std
::
cout
<<
"Server listening on port "
<<
PORT
<<
std
::
endl
;
while
(
true
)
{
client_socket
=
accept
(
server_fd
,
(
struct
sockaddr
*
)
&
address
,
&
addrlen
);
if
(
client_socket
<
0
)
{
perror
(
"Accept failed"
);
continue
;
}
std
::
cout
<<
"New client connected"
<<
std
::
endl
;
// Starte neuen Thread für den Client
threads
.
emplace_back
(
handle_client
,
client_socket
);
}
// Alle Threads beenden
for
(
auto
&
t
:
threads
)
{
t
.
join
();
}
close
(
server_fd
);
return
0
;
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment