Add files for task 06
This commit is contained in:
parent
ba4ba97158
commit
78c59aa3f8
6 changed files with 344 additions and 0 deletions
BIN
Tasks/Lab6/DT6.pdf
Normal file
BIN
Tasks/Lab6/DT6.pdf
Normal file
Binary file not shown.
29
Tasks/Lab6/DT6_0.cs
Normal file
29
Tasks/Lab6/DT6_0.cs
Normal file
|
@ -0,0 +1,29 @@
|
||||||
|
using System;
|
||||||
|
using System.Net;
|
||||||
|
using System.Net.Sockets;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace DT6 {
|
||||||
|
// Client app is the one sending messages to a Server/listener.
|
||||||
|
// Both listener and client can send messages back and forth once a
|
||||||
|
// communication is established.
|
||||||
|
public class SocketClient {
|
||||||
|
|
||||||
|
public static int Main(String[] args) {
|
||||||
|
Received += (arg) => Console.Write("Received:" + arg);
|
||||||
|
Task.Run(() => StartClient());
|
||||||
|
Thread.Sleep(1000);
|
||||||
|
Console.ReadKey();
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static event Action<string> Received;
|
||||||
|
|
||||||
|
public static void StartClient() {
|
||||||
|
/* TO BE DONE
|
||||||
|
*/
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
85
Tasks/Lab6/SocketClient.cs
Normal file
85
Tasks/Lab6/SocketClient.cs
Normal file
|
@ -0,0 +1,85 @@
|
||||||
|
|
||||||
|
using System;
|
||||||
|
using System.Net;
|
||||||
|
using System.Net.Sockets;
|
||||||
|
using System.Text;
|
||||||
|
|
||||||
|
// Client app is the one sending messages to a Server/listener.
|
||||||
|
// Both listener and client can send messages back and forth once a
|
||||||
|
// communication is established.
|
||||||
|
public class SocketClient
|
||||||
|
{
|
||||||
|
public static int Main(String[] args)
|
||||||
|
{
|
||||||
|
StartClient();
|
||||||
|
Console.WriteLine("\n Press any key to continue...");
|
||||||
|
Console.ReadKey();
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public static void StartClient()
|
||||||
|
{
|
||||||
|
byte[] bytes = new byte[1024];
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
// Connect to a Remote server
|
||||||
|
// Get Host IP Address that is used to establish a connection
|
||||||
|
// In this case, we get one IP address of localhost that is IP : 127.0.0.1
|
||||||
|
// If a host has multiple addresses, you will get a list of addresses
|
||||||
|
//IPHostEntry host = Dns.GetHostEntry("localhost");
|
||||||
|
//IPAddress ipAddress = host.AddressList[0];
|
||||||
|
|
||||||
|
IPAddress ipAddress = IPAddress.Parse("127.0.0.1");
|
||||||
|
IPEndPoint remoteEP = new IPEndPoint(ipAddress, 11000);
|
||||||
|
|
||||||
|
// Create a TCP/IP socket.
|
||||||
|
Socket sender = new Socket(ipAddress.AddressFamily,
|
||||||
|
SocketType.Stream, ProtocolType.Tcp);
|
||||||
|
|
||||||
|
// Connect the socket to the remote endpoint. Catch any errors.
|
||||||
|
try
|
||||||
|
{
|
||||||
|
// Connect to Remote EndPoint
|
||||||
|
sender.Connect(remoteEP);
|
||||||
|
|
||||||
|
Console.WriteLine("Socket connected to {0}",
|
||||||
|
sender.RemoteEndPoint.ToString());
|
||||||
|
|
||||||
|
// Encode the data string into a byte array.
|
||||||
|
byte[] msg = Encoding.ASCII.GetBytes("This is a test<EOF>");
|
||||||
|
|
||||||
|
// Send the data through the socket.
|
||||||
|
int bytesSent = sender.Send(msg);
|
||||||
|
|
||||||
|
// Receive the response from the remote device.
|
||||||
|
int bytesRec = sender.Receive(bytes);
|
||||||
|
Console.WriteLine("Echoed test = {0}",
|
||||||
|
Encoding.ASCII.GetString(bytes, 0, bytesRec));
|
||||||
|
|
||||||
|
// Release the socket.
|
||||||
|
sender.Shutdown(SocketShutdown.Both);
|
||||||
|
sender.Close();
|
||||||
|
|
||||||
|
}
|
||||||
|
catch (ArgumentNullException ane)
|
||||||
|
{
|
||||||
|
Console.WriteLine("ArgumentNullException : {0}", ane.ToString());
|
||||||
|
}
|
||||||
|
catch (SocketException se)
|
||||||
|
{
|
||||||
|
Console.WriteLine("SocketException : {0}", se.ToString());
|
||||||
|
}
|
||||||
|
catch (Exception e)
|
||||||
|
{
|
||||||
|
Console.WriteLine("Unexpected exception : {0}", e.ToString());
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
catch (Exception e)
|
||||||
|
{
|
||||||
|
Console.WriteLine(e.ToString());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
71
Tasks/Lab6/SocketListener.cs
Normal file
71
Tasks/Lab6/SocketListener.cs
Normal file
|
@ -0,0 +1,71 @@
|
||||||
|
|
||||||
|
using System;
|
||||||
|
using System.Net;
|
||||||
|
using System.Net.Sockets;
|
||||||
|
using System.Text;
|
||||||
|
|
||||||
|
// Socket Listener acts as a server and listens to the incoming
|
||||||
|
// messages on the specified port and protocol.
|
||||||
|
public class SocketListener
|
||||||
|
{
|
||||||
|
public static int Main(String[] args)
|
||||||
|
{
|
||||||
|
StartServer();
|
||||||
|
Console.WriteLine("\n Press any key to continue...");
|
||||||
|
Console.ReadKey();
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public static void StartServer()
|
||||||
|
{
|
||||||
|
// Get Host IP Address that is used to establish a connection
|
||||||
|
// In this case, we get one IP address of localhost that is IP : 127.0.0.1
|
||||||
|
// If a host has multiple addresses, you will get a list of addresses
|
||||||
|
// IPHostEntry host = Dns.GetHostEntry("localhost");
|
||||||
|
// IPAddress ipAddress = host.AddressList[0];
|
||||||
|
IPAddress ipAddress = IPAddress.Parse("127.0.0.1");
|
||||||
|
IPEndPoint localEndPoint = new IPEndPoint(ipAddress, 11000);
|
||||||
|
|
||||||
|
|
||||||
|
try {
|
||||||
|
|
||||||
|
// Create a Socket that will use Tcp protocol
|
||||||
|
Socket listener = new Socket(ipAddress.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
|
||||||
|
// A Socket must be associated with an endpoint using the Bind method
|
||||||
|
listener.Bind(localEndPoint);
|
||||||
|
// Specify how many requests a Socket can listen before it gives Server busy response.
|
||||||
|
// We will listen 10 requests at a time
|
||||||
|
listener.Listen(10);
|
||||||
|
|
||||||
|
Console.WriteLine("Waiting for a connection...");
|
||||||
|
Socket handler = listener.Accept();
|
||||||
|
|
||||||
|
// Incoming data from the client.
|
||||||
|
string data = null;
|
||||||
|
byte[] bytes = null;
|
||||||
|
|
||||||
|
while (true)
|
||||||
|
{
|
||||||
|
bytes = new byte[1024];
|
||||||
|
int bytesRec = handler.Receive(bytes);
|
||||||
|
data += Encoding.ASCII.GetString(bytes, 0, bytesRec);
|
||||||
|
if (data.IndexOf("<EOF>") > -1)
|
||||||
|
{
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Console.WriteLine("Text received : {0}", data);
|
||||||
|
|
||||||
|
byte[] msg = Encoding.ASCII.GetBytes(data);
|
||||||
|
handler.Send(msg);
|
||||||
|
handler.Shutdown(SocketShutdown.Both);
|
||||||
|
handler.Close();
|
||||||
|
}
|
||||||
|
catch (Exception e)
|
||||||
|
{
|
||||||
|
Console.WriteLine(e.ToString());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
159
Tasks/Lab6/server5.c
Normal file
159
Tasks/Lab6/server5.c
Normal file
|
@ -0,0 +1,159 @@
|
||||||
|
// Server side C/C++ program to demonstrate Socket programming
|
||||||
|
// adopted by Karl Rege from sample of
|
||||||
|
// https://www.geeksforgeeks.org/socket-programming-cc/
|
||||||
|
// for windows: Link with ws2_32 library.
|
||||||
|
// gcc server.c -lws2_32 -o server.exe
|
||||||
|
// platform: Linux and Windows
|
||||||
|
|
||||||
|
#ifdef _MSC_VER
|
||||||
|
#include <Ws2tcpip.h>
|
||||||
|
#include <Mswsock.h>
|
||||||
|
#else
|
||||||
|
#include <unistd.h>
|
||||||
|
#endif
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
#ifdef WIN32
|
||||||
|
#include <Winsock2.h>
|
||||||
|
#include <windows.h>
|
||||||
|
#include <wininet.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
typedef int socklen_t;
|
||||||
|
#else
|
||||||
|
#include <unistd.h>
|
||||||
|
#include <errno.h>
|
||||||
|
#include <sys/socket.h>
|
||||||
|
#include <sys/ioctl.h>
|
||||||
|
#include <netinet/in.h>
|
||||||
|
#include <netinet/tcp.h>
|
||||||
|
#include <netdb.h>
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#define BUFSIZE 1024
|
||||||
|
#define PORT 8000
|
||||||
|
char* rank[10] = {
|
||||||
|
"Mueller Stefan;02:31:14\n",
|
||||||
|
"Marti Adrian;02:30:09\n",
|
||||||
|
"Kiptum Daniel;02:11:31\n",
|
||||||
|
"Ancay Tarcis;02:20:02\n",
|
||||||
|
"Kreibuhl Christian;02:21:47\n",
|
||||||
|
"Ott Michael;02:33:48\n",
|
||||||
|
"Menzi Christoph;02:27:26\n",
|
||||||
|
"Oliver Ruben;02:32:12\n",
|
||||||
|
"Elmer Beat;02:33:53\n",
|
||||||
|
"Kuehni Martin;02:33:36\n"
|
||||||
|
};
|
||||||
|
#ifdef WIN32
|
||||||
|
char* GetErrorMessage(DWORD dwErrorCode) {
|
||||||
|
static char* pBuffer = NULL;
|
||||||
|
DWORD cchBufferLength = 200;
|
||||||
|
if (pBuffer == NULL) { // only allocate once
|
||||||
|
pBuffer = malloc(cchBufferLength*sizeof(char));
|
||||||
|
}
|
||||||
|
pBuffer[0] = '\0';
|
||||||
|
DWORD cchMsg = FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
|
||||||
|
NULL, dwErrorCode, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
|
||||||
|
pBuffer, cchBufferLength, NULL);
|
||||||
|
return pBuffer ;
|
||||||
|
}
|
||||||
|
#else
|
||||||
|
int GetLastError() {
|
||||||
|
return errno;
|
||||||
|
}
|
||||||
|
char* GetErrorMessage(int dwErrorCode) {
|
||||||
|
return strerror(dwErrorCode);
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
void sleep_ms(int milliseconds) { // cross-platform sleep function
|
||||||
|
#ifdef WIN32
|
||||||
|
Sleep(milliseconds);
|
||||||
|
#else
|
||||||
|
usleep(milliseconds * 1000);
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
void error(char* msg){
|
||||||
|
int err = GetLastError();
|
||||||
|
fprintf(stderr,"%s \n",msg);
|
||||||
|
fprintf(stderr,"ERROR %d %s \n",err, GetErrorMessage(err));
|
||||||
|
}
|
||||||
|
|
||||||
|
// send data immediately but degrades performance
|
||||||
|
void send_nodelay(int sock, char* msg, int len, int d) {
|
||||||
|
int flag = 1;
|
||||||
|
setsockopt(sock, IPPROTO_TCP, TCP_NODELAY, (char *) &flag, sizeof(int));
|
||||||
|
send(sock, msg, len , d);
|
||||||
|
flag = 0;
|
||||||
|
setsockopt(sock, IPPROTO_TCP, TCP_NODELAY, (char *) &flag, sizeof(int));
|
||||||
|
sleep_ms(1); // to give the socket thread the time to send
|
||||||
|
}
|
||||||
|
|
||||||
|
int main(int argc, char const *argv[]) {
|
||||||
|
int server_fd, sock, valread;
|
||||||
|
struct sockaddr_in address;
|
||||||
|
int i, opt = 1;
|
||||||
|
int addrlen = sizeof(address);
|
||||||
|
char buffer[BUFSIZE] = {0};
|
||||||
|
|
||||||
|
#ifdef WIN32
|
||||||
|
// Initialize Winsock
|
||||||
|
WSADATA wsaData;
|
||||||
|
if (WSAStartup(MAKEWORD(2,2), &wsaData) != 0) {
|
||||||
|
error("init failed");
|
||||||
|
exit(EXIT_FAILURE);
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
// Creating socket file descriptor
|
||||||
|
if ((server_fd = socket(AF_INET, SOCK_STREAM, 0)) == 0) {
|
||||||
|
error("socket failed");
|
||||||
|
exit(EXIT_FAILURE);
|
||||||
|
}
|
||||||
|
|
||||||
|
address.sin_family = AF_INET;
|
||||||
|
address.sin_addr.s_addr = INADDR_ANY;
|
||||||
|
address.sin_port = htons(PORT);
|
||||||
|
|
||||||
|
// Forcefully attaching socket to the port
|
||||||
|
if (bind(server_fd, (struct sockaddr *)&address, sizeof(address))<0) {
|
||||||
|
error("bind failed");
|
||||||
|
exit(EXIT_FAILURE);
|
||||||
|
}
|
||||||
|
printf("listen on port %d... \n",PORT);
|
||||||
|
if (listen(server_fd, 3) < 0) {
|
||||||
|
error("listen failed");
|
||||||
|
exit(EXIT_FAILURE);
|
||||||
|
}
|
||||||
|
|
||||||
|
while (1) {
|
||||||
|
printf("accepting ...\n");
|
||||||
|
fflush(stdout);
|
||||||
|
if ((sock = accept(server_fd, (struct sockaddr *)&address, (socklen_t*)&addrlen))<0) {
|
||||||
|
error("accept failed");
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
// send line
|
||||||
|
#if defined(WIN32) || !defined(FILEDESC)
|
||||||
|
for (i = 0; i < 10; i++) {
|
||||||
|
send_nodelay(sock , rank[i] , strlen(rank[i]) , 0);
|
||||||
|
printf (".");
|
||||||
|
}
|
||||||
|
#else
|
||||||
|
int fd_copy = dup(sock);
|
||||||
|
FILE *write_fh = fdopen(fd_copy, "w");
|
||||||
|
int i;
|
||||||
|
for (i = 0; i < 10; i++) {
|
||||||
|
fprintf(write_fh,"%s", rank[i]);
|
||||||
|
fflush(write_fh);
|
||||||
|
sleep_ms(1);
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
printf("\nSENT: %d lines\n",i);
|
||||||
|
sleep_ms(100);
|
||||||
|
shutdown (sock,2);
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
BIN
Tasks/Lab6/server5.exe
Normal file
BIN
Tasks/Lab6/server5.exe
Normal file
Binary file not shown.
Loading…
Reference in a new issue