forked from s444341/Projekt_SIK
pierwsza wersja
This commit is contained in:
parent
cc92fb5309
commit
80f6635036
3
.gitignore
vendored
Normal file
3
.gitignore
vendored
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
.vscode
|
||||||
|
target
|
||||||
|
server/server
|
19
README.md
19
README.md
@ -1,7 +1,20 @@
|
|||||||
# Projekt_SIK
|
# Projekt_SIK
|
||||||
|
|
||||||
Projekt 1 Sieci Komputerowe
|
Projekt 1 Sieci Komputerowe <br>
|
||||||
|
Wykorzystujemy protokol TCP! <br>
|
||||||
|
|
||||||
klient w javie
|
W gitignore dodalem pliki ktore nie maja byc przesylane na repozytorium. U mnie sa to smieci produkowane z Visual Studio Code oraz folder target z javy - tam jest generowany plik jar przez maven (jak zrobi sie maven install), mozna to robic lokalnie, nie trzeba przesylac na repozytorium. Dodatkowo nie przesylamy skompilowanego pliku z C czyli server/server.
|
||||||
|
|
||||||
serwer w C
|
## klient
|
||||||
|
W javie. Wykorzystalem to narzedzie maven, na Inzynierii oprogramowania je uruchomil. W skrocie sluzy do tego zebysmy potem mogli ladnie wyeksportowac plik javy do jednego pliku ktory mozemy uruchomic - takie narzedzie co kompiluje nam jave pozniej. Caly kod jest w App.java, wysylanie wiadomosci dziala - uwaga na wielkosc wiadomosci - te bajty. Bedziemy musieli chyba zrobic tak ze wiadomosci tekstowe sa rozdzielane na takie segmenty odpowiedniej wielkosci zeby pozniej je ladnie wyslac bo niestety C w ktorym jest napisany serwer musi miec dokladnie okreslona wielkosc odbieranej wiadomosci.
|
||||||
|
|
||||||
|
## serwer
|
||||||
|
W C. <br>
|
||||||
|
Done:
|
||||||
|
* laczenie sie z pojedynczym klientem
|
||||||
|
* odbieranie wiadomosci tekstowych od jednego klienta i wyswietlanie je w konsoli
|
||||||
|
|
||||||
|
TODO:
|
||||||
|
* polaczenie od wielu klientow
|
||||||
|
* odbieranie duzych plikow
|
||||||
|
* przekazywanie wiadomosci do klienta
|
||||||
|
44
klient/.classpath
Normal file
44
klient/.classpath
Normal file
@ -0,0 +1,44 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<classpath>
|
||||||
|
<classpathentry kind="src" output="target/classes" path="src/main/java">
|
||||||
|
<attributes>
|
||||||
|
<attribute name="optional" value="true"/>
|
||||||
|
<attribute name="maven.pomderived" value="true"/>
|
||||||
|
</attributes>
|
||||||
|
</classpathentry>
|
||||||
|
<classpathentry kind="src" output="target/test-classes" path="src/test/java">
|
||||||
|
<attributes>
|
||||||
|
<attribute name="optional" value="true"/>
|
||||||
|
<attribute name="maven.pomderived" value="true"/>
|
||||||
|
<attribute name="test" value="true"/>
|
||||||
|
</attributes>
|
||||||
|
</classpathentry>
|
||||||
|
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.7">
|
||||||
|
<attributes>
|
||||||
|
<attribute name="maven.pomderived" value="true"/>
|
||||||
|
</attributes>
|
||||||
|
</classpathentry>
|
||||||
|
<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
|
||||||
|
<attributes>
|
||||||
|
<attribute name="maven.pomderived" value="true"/>
|
||||||
|
</attributes>
|
||||||
|
</classpathentry>
|
||||||
|
<classpathentry kind="src" path="target/generated-sources/annotations">
|
||||||
|
<attributes>
|
||||||
|
<attribute name="optional" value="true"/>
|
||||||
|
<attribute name="maven.pomderived" value="true"/>
|
||||||
|
<attribute name="ignore_optional_problems" value="true"/>
|
||||||
|
<attribute name="m2e-apt" value="true"/>
|
||||||
|
</attributes>
|
||||||
|
</classpathentry>
|
||||||
|
<classpathentry kind="src" output="target/test-classes" path="target/generated-test-sources/test-annotations">
|
||||||
|
<attributes>
|
||||||
|
<attribute name="optional" value="true"/>
|
||||||
|
<attribute name="maven.pomderived" value="true"/>
|
||||||
|
<attribute name="ignore_optional_problems" value="true"/>
|
||||||
|
<attribute name="m2e-apt" value="true"/>
|
||||||
|
<attribute name="test" value="true"/>
|
||||||
|
</attributes>
|
||||||
|
</classpathentry>
|
||||||
|
<classpathentry kind="output" path="target/classes"/>
|
||||||
|
</classpath>
|
23
klient/.project
Normal file
23
klient/.project
Normal file
@ -0,0 +1,23 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<projectDescription>
|
||||||
|
<name>Projekt</name>
|
||||||
|
<comment></comment>
|
||||||
|
<projects>
|
||||||
|
</projects>
|
||||||
|
<buildSpec>
|
||||||
|
<buildCommand>
|
||||||
|
<name>org.eclipse.jdt.core.javabuilder</name>
|
||||||
|
<arguments>
|
||||||
|
</arguments>
|
||||||
|
</buildCommand>
|
||||||
|
<buildCommand>
|
||||||
|
<name>org.eclipse.m2e.core.maven2Builder</name>
|
||||||
|
<arguments>
|
||||||
|
</arguments>
|
||||||
|
</buildCommand>
|
||||||
|
</buildSpec>
|
||||||
|
<natures>
|
||||||
|
<nature>org.eclipse.jdt.core.javanature</nature>
|
||||||
|
<nature>org.eclipse.m2e.core.maven2Nature</nature>
|
||||||
|
</natures>
|
||||||
|
</projectDescription>
|
4
klient/.settings/org.eclipse.core.resources.prefs
Normal file
4
klient/.settings/org.eclipse.core.resources.prefs
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
eclipse.preferences.version=1
|
||||||
|
encoding//src/main/java=UTF-8
|
||||||
|
encoding//src/test/java=UTF-8
|
||||||
|
encoding/<project>=UTF-8
|
2
klient/.settings/org.eclipse.jdt.apt.core.prefs
Normal file
2
klient/.settings/org.eclipse.jdt.apt.core.prefs
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
eclipse.preferences.version=1
|
||||||
|
org.eclipse.jdt.apt.aptEnabled=false
|
9
klient/.settings/org.eclipse.jdt.core.prefs
Normal file
9
klient/.settings/org.eclipse.jdt.core.prefs
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
eclipse.preferences.version=1
|
||||||
|
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.7
|
||||||
|
org.eclipse.jdt.core.compiler.compliance=1.7
|
||||||
|
org.eclipse.jdt.core.compiler.problem.enablePreviewFeatures=disabled
|
||||||
|
org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning
|
||||||
|
org.eclipse.jdt.core.compiler.problem.reportPreviewFeatures=ignore
|
||||||
|
org.eclipse.jdt.core.compiler.processAnnotations=disabled
|
||||||
|
org.eclipse.jdt.core.compiler.release=disabled
|
||||||
|
org.eclipse.jdt.core.compiler.source=1.7
|
4
klient/.settings/org.eclipse.m2e.core.prefs
Normal file
4
klient/.settings/org.eclipse.m2e.core.prefs
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
activeProfiles=
|
||||||
|
eclipse.preferences.version=1
|
||||||
|
resolveWorkspaceProjects=true
|
||||||
|
version=1
|
74
klient/pom.xml
Normal file
74
klient/pom.xml
Normal file
@ -0,0 +1,74 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
|
||||||
|
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
|
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||||
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
|
||||||
|
<groupId>SieciKomp</groupId>
|
||||||
|
<artifactId>Projekt</artifactId>
|
||||||
|
<version>Final</version>
|
||||||
|
|
||||||
|
<name>Projekt</name>
|
||||||
|
<url>https://git.wmi.amu.edu.pl/s444341/Projekt_SIK</url>
|
||||||
|
|
||||||
|
<properties>
|
||||||
|
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||||
|
<maven.compiler.source>1.7</maven.compiler.source>
|
||||||
|
<maven.compiler.target>1.7</maven.compiler.target>
|
||||||
|
</properties>
|
||||||
|
|
||||||
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>junit</groupId>
|
||||||
|
<artifactId>junit</artifactId>
|
||||||
|
<version>4.11</version>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
|
||||||
|
<build>
|
||||||
|
<pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
|
||||||
|
<plugins>
|
||||||
|
<!-- clean lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#clean_Lifecycle -->
|
||||||
|
<plugin>
|
||||||
|
<artifactId>maven-clean-plugin</artifactId>
|
||||||
|
<version>3.1.0</version>
|
||||||
|
</plugin>
|
||||||
|
<!-- default lifecycle, jar packaging: see https://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging -->
|
||||||
|
<plugin>
|
||||||
|
<artifactId>maven-resources-plugin</artifactId>
|
||||||
|
<version>3.0.2</version>
|
||||||
|
</plugin>
|
||||||
|
<plugin>
|
||||||
|
<artifactId>maven-compiler-plugin</artifactId>
|
||||||
|
<version>3.8.0</version>
|
||||||
|
</plugin>
|
||||||
|
<plugin>
|
||||||
|
<artifactId>maven-surefire-plugin</artifactId>
|
||||||
|
<version>2.22.1</version>
|
||||||
|
</plugin>
|
||||||
|
<plugin>
|
||||||
|
<artifactId>maven-jar-plugin</artifactId>
|
||||||
|
<version>3.0.2</version>
|
||||||
|
</plugin>
|
||||||
|
<plugin>
|
||||||
|
<artifactId>maven-install-plugin</artifactId>
|
||||||
|
<version>2.5.2</version>
|
||||||
|
</plugin>
|
||||||
|
<plugin>
|
||||||
|
<artifactId>maven-deploy-plugin</artifactId>
|
||||||
|
<version>2.8.2</version>
|
||||||
|
</plugin>
|
||||||
|
<!-- site lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#site_Lifecycle -->
|
||||||
|
<plugin>
|
||||||
|
<artifactId>maven-site-plugin</artifactId>
|
||||||
|
<version>3.7.1</version>
|
||||||
|
</plugin>
|
||||||
|
<plugin>
|
||||||
|
<artifactId>maven-project-info-reports-plugin</artifactId>
|
||||||
|
<version>3.0.0</version>
|
||||||
|
</plugin>
|
||||||
|
</plugins>
|
||||||
|
</pluginManagement>
|
||||||
|
</build>
|
||||||
|
</project>
|
41
klient/src/main/java/Projekt/App.java
Normal file
41
klient/src/main/java/Projekt/App.java
Normal file
@ -0,0 +1,41 @@
|
|||||||
|
package Projekt;
|
||||||
|
|
||||||
|
import java.io.*;
|
||||||
|
import java.net.*;
|
||||||
|
|
||||||
|
public class App
|
||||||
|
{
|
||||||
|
public static void main(String[] args) {
|
||||||
|
try {
|
||||||
|
// USTALAMY adres serwera i port - trzeba wpisac to co wyrzuca po uruchomieniu serwer
|
||||||
|
InetAddress addr = InetAddress.getByName("0.0.0.0");
|
||||||
|
int port = 44444; //Integer.parseInt(args[1]);
|
||||||
|
|
||||||
|
// utworz gniazdo i od razu podlacz je do addr:port
|
||||||
|
Socket socket = new Socket(addr, port);
|
||||||
|
|
||||||
|
//strumienie
|
||||||
|
DataOutputStream dos = new DataOutputStream(socket.getOutputStream()); //tu wysylamy wiadomosci
|
||||||
|
DataInputStream dis = new DataInputStream(socket.getInputStream()); //tu bedziemy przyjmowac wiadomosci
|
||||||
|
|
||||||
|
//wiadomosc musimy stworzyc tak zeby miala 1024 bajty - tak zeby pasowala do tablicy charow z serwera!
|
||||||
|
byte[] mess = new byte[1024];
|
||||||
|
String str = "Hello world from java!";
|
||||||
|
mess = str.getBytes(); //konwertujemy na bajty
|
||||||
|
dos.write(mess); //wysylamy
|
||||||
|
|
||||||
|
|
||||||
|
// czytaj odpowiedz to jest kod golinskiego
|
||||||
|
//String s = dis.readUTF();
|
||||||
|
// wypisz odpowiedz
|
||||||
|
//System.out.println("Serwer powiedzial: "+s);
|
||||||
|
dis.close();
|
||||||
|
dos.close();
|
||||||
|
socket.close();
|
||||||
|
|
||||||
|
} catch (Exception e) { //kiepskie lapanie wyjatkow!! lepiej bardziej sprecyzowac jaki wyjatek lapiemy
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
System.out.println("Klient zakonczyl dzialanie");
|
||||||
|
}
|
||||||
|
}
|
20
klient/src/test/java/Projekt/AppTest.java
Normal file
20
klient/src/test/java/Projekt/AppTest.java
Normal file
@ -0,0 +1,20 @@
|
|||||||
|
package Projekt;
|
||||||
|
|
||||||
|
import static org.junit.Assert.assertTrue;
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Unit test for simple App.
|
||||||
|
*/
|
||||||
|
public class AppTest
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Rigorous Test :-)
|
||||||
|
*/
|
||||||
|
@Test
|
||||||
|
public void shouldAnswerWithTrue()
|
||||||
|
{
|
||||||
|
assertTrue( true );
|
||||||
|
}
|
||||||
|
}
|
39
server/server.c
Normal file
39
server/server.c
Normal file
@ -0,0 +1,39 @@
|
|||||||
|
#include <sys/types.h>
|
||||||
|
#include <sys/socket.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <netinet/in.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <arpa/inet.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
|
||||||
|
int main(void) {
|
||||||
|
char bufor[1024]; //wiadomosc
|
||||||
|
int gniazdo, gniazdo2;
|
||||||
|
struct sockaddr_in adr, nadawca;
|
||||||
|
socklen_t dl = sizeof(struct sockaddr_in);
|
||||||
|
|
||||||
|
gniazdo = socket(PF_INET, SOCK_STREAM, 0);
|
||||||
|
adr.sin_family = AF_INET;
|
||||||
|
adr.sin_port = htons(44444); //port
|
||||||
|
adr.sin_addr.s_addr = INADDR_ANY;
|
||||||
|
|
||||||
|
printf("Slucham na %s:%d\n", inet_ntoa(adr.sin_addr), ntohs(adr.sin_port));
|
||||||
|
|
||||||
|
if (bind(gniazdo, (struct sockaddr*) &adr, sizeof(adr)) < 0) { //bind
|
||||||
|
printf("Bind nie powiodl sie.\n");
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
if (listen(gniazdo, 10) < 0) { //listen
|
||||||
|
printf("Listen nie powiodl sie.\n");
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
printf("Czekam na polaczenie ...\n");
|
||||||
|
while ((gniazdo2 = accept(gniazdo, (struct sockaddr*) &nadawca, &dl)) > 0) { //odbieranie
|
||||||
|
memset(bufor, 0, 1024);
|
||||||
|
recv(gniazdo2, bufor, 1024, 0);
|
||||||
|
printf("Wiadomosc od %s: %s\n", inet_ntoa(nadawca.sin_addr), bufor);
|
||||||
|
close(gniazdo2);
|
||||||
|
}
|
||||||
|
close(gniazdo);
|
||||||
|
return 0;
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user