emailconfirmed, nsInternRO, nsInternRW, Administrators
3,356
edits
(sockets) |
mNo edit summary |
||
Line 60: | Line 60: | ||
* [[OSC]] - Open Sound Control | * [[OSC]] - Open Sound Control | ||
== Sockets == | == APIs == | ||
=== Sockets === | |||
Protokolle wie TCP und UDP sind abstrakte Dienste, die die Struktur von Computer-Kommunikation definieren. Programmierer benutzen für TCP und UDP Operationen ''[http://de.wikipedia.org/wiki/Socket_(Software) Sockets]'', manchmal auch BSD-Sockets genannt. Sockets sind in jedem Betriebssystem zu finden. | Protokolle wie TCP und UDP sind abstrakte Dienste, die die Struktur von Computer-Kommunikation definieren. Programmierer benutzen für TCP und UDP Operationen ''[http://de.wikipedia.org/wiki/Socket_(Software) Sockets]'', manchmal auch BSD-Sockets genannt. Sockets sind in jedem Betriebssystem zu finden. | ||
'''Sockets in JAVA (Beispiel)''' | |||
<source lang="java"> | <source lang="java"> | ||
ServerSocket serverSocket = new ServerSocket(port); // Serversocket mit bestimmter Port-Nummer erstellen | ServerSocket serverSocket = new ServerSocket(port); // Serversocket mit bestimmter Port-Nummer erstellen | ||
Line 78: | Line 79: | ||
</source> | </source> | ||
<br/> | |||
=== PHP Socket === | |||
<source lang="PHP"> | |||
<? | |||
// code von | |||
// http://www.java2s.com/Code/Php/Network/CreatesaROT13serverwhenpeopleconnecttoitandsendtextitrespondswiththeROT13equivalentoftheirtext.htm | |||
$socket = socket_create_listen("12345"); | |||
if (!$socket) { | |||
print "Failed to create socket!\n"; | |||
exit; | |||
} | |||
while (true) { | |||
$client = socket_accept($socket); | |||
$welcome = "\nWelcome\n"; | |||
socket_write($client, $welcome); | |||
while (true) { | |||
$input = trim(socket_read ($client, 256)); | |||
if ($input = = '!close') { break; } | |||
if ($input = = '!halt') { | |||
socket_close ($client); | |||
break 2; | |||
} | |||
$output = str_rot13($input) . "\n"; | |||
socket_write($client, $output); | |||
print "Them: $input, Us: $output\n"; | |||
} | |||
socket_close ($client); | |||
} | |||
socket_close ($socket); | |||
?> | |||
</source> | |||
weiterführende Links: | weiterführende Links: | ||
* [http://de.wikipedia.org/wiki/Socket_(Software) BSD Sockets] | * [http://de.wikipedia.org/wiki/Socket_(Software) BSD Sockets] | ||
* weitere Beispiele (z.B. für Processing) sind unter [[OSC]] zu finden! | |||
=== Datagram === | |||
<br/> | |||
'''Datagram Socket (JAVA)''' | |||
<source lang="java"> | |||
// code von | |||
// http://www.java2s.com/Code/Java/Network-Protocol/UseDatagramSockettosendoutandreceiveDatagramPacket.htm | |||
import java.io.BufferedReader; | |||
import java.io.IOException; | |||
import java.io.InputStreamReader; | |||
import java.net.DatagramPacket; | |||
import java.net.DatagramSocket; | |||
import java.net.InetAddress; | |||
public class ChatClient { | |||
public static void main(String[] args) throws Exception { | |||
DatagramSocket s = new DatagramSocket(); | |||
byte[] buf = new byte[1000]; | |||
DatagramPacket dp = new DatagramPacket(buf, buf.length); | |||
InetAddress hostAddress = InetAddress.getByName("localhost"); | |||
while (true) { | |||
BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in)); | |||
String outMessage = stdin.readLine(); | |||
if (outMessage.equals("bye")) | |||
break; | |||
String outString = "Client say: " + outMessage; | |||
buf = outString.getBytes(); | |||
DatagramPacket out = new DatagramPacket(buf, buf.length, hostAddress, 9999); | |||
s.send(out); | |||
s.receive(dp); | |||
String rcvd = "rcvd from " + dp.getAddress() + ", " + dp.getPort() + ": " | |||
+ new String(dp.getData(), 0, dp.getLength()); | |||
System.out.println(rcvd); | |||
} | |||
} | |||
} | |||
import java.io.BufferedReader; | |||
import java.io.InputStreamReader; | |||
import java.net.DatagramPacket; | |||
import java.net.DatagramSocket; | |||
public class ChatServer { | |||
public static void main(String[] args) throws Exception { | |||
int PORT = 4000; | |||
byte[] buf = new byte[1000]; | |||
DatagramPacket dgp = new DatagramPacket(buf, buf.length); | |||
DatagramSocket sk; | |||
sk = new DatagramSocket(PORT); | |||
System.out.println("Server started"); | |||
while (true) { | |||
sk.receive(dgp); | |||
String rcvd = new String(dgp.getData(), 0, dgp.getLength()) + ", from address: " | |||
+ dgp.getAddress() + ", port: " + dgp.getPort(); | |||
System.out.println(rcvd); | |||
BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in)); | |||
String outMessage = stdin.readLine(); | |||
buf = ("Server say: " + outMessage).getBytes(); | |||
DatagramPacket out = new DatagramPacket(buf, buf.length, dgp.getAddress(), dgp.getPort()); | |||
sk.send(out); | |||
} | |||
} | |||
} | |||
</source> | |||
== Links == | == Links == | ||
siehe "weiterführende Links" unter den einzelnen Sektionen und: | siehe "weiterführende Links" unter den einzelnen Sektionen und: | ||
* [http://www.uni-weimar.de/cms/medien/webis/teaching/lecture-notes.html#web-technology Web-Technologie Vorlesung] bei Prof. Benno Stein | |||
* [http://www.uni-weimar.de/medien/webis/teaching/lecturenotes/web-technology/unit-de-network-protocol1.pdf Lecture Notes Network Protocols 1] von Prof. Benno Stein | * [http://www.uni-weimar.de/medien/webis/teaching/lecturenotes/web-technology/unit-de-network-protocol1.pdf Lecture Notes Network Protocols 1] von Prof. Benno Stein | ||
* [http://www.uni-weimar.de/medien/webis/teaching/lecturenotes/web-technology/unit-de-network-protocol1.pdf Lecture Notes Network Protocols 2] von Prof. Benno Stein | * [http://www.uni-weimar.de/medien/webis/teaching/lecturenotes/web-technology/unit-de-network-protocol1.pdf Lecture Notes Network Protocols 2] von Prof. Benno Stein | ||
[[Category:Programmiersprachen]] | |||
[[Category:Internet]] | [[Category:Internet]] | ||
[[Category:Netztechnik]] | [[Category:Netztechnik]] |