Clienti C, server Java, transmiterea unui sir de caractere:
client.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 |
#include <sys/types.h> #include <sys/socket.h> #include <stdio.h> #include <netinet/in.h> #include <netinet/ip.h> #include <string.h> int main() { int c; struct sockaddr_in server; char buffer[100]; c = socket(AF_INET, SOCK_STREAM, 0); if (c < 0) { printf("Eroare la crearea socketului client\n"); return 1; } memset(&server, 0, sizeof(server)); server.sin_port = htons(1234); server.sin_family = AF_INET; server.sin_addr.s_addr = inet_addr("127.0.0.1"); if (connect(c, (struct sockaddr *) &server, sizeof(server)) < 0) { printf("Eroare la conectarea la server\n"); return 1; } printf("Dati un string: "); fgets(buffer, 100, stdin); send(c, buffer, strlen(buffer), 0); close(c); } |
Server.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
import java.net.*; import java.io.*; public class Server { public static void main(String args[]) throws Exception { ServerSocket s = new ServerSocket(1234); byte b[] = new byte[100]; while(true) { Socket c = s.accept(); System.out.println("Client connected!"); c.getInputStream().read(b); System.out.println(new String(b)); c.close(); } } } |