0% found this document useful (0 votes)
20 views

C JAVA Sockets Examples

The document provides code examples of UDP and TCP sockets in C and Java for client-server communication. In C, struct sockaddr_in must be populated to create connections between peers, and processes must explicitly bind to ports. In Java, socket creation, binding and listening are simplified using ServerSocket. The examples demonstrate basic client-server communication over UDP and TCP in both languages.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views

C JAVA Sockets Examples

The document provides code examples of UDP and TCP sockets in C and Java for client-server communication. In C, struct sockaddr_in must be populated to create connections between peers, and processes must explicitly bind to ports. In Java, socket creation, binding and listening are simplified using ServerSocket. The examples demonstrate basic client-server communication over UDP and TCP in both languages.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

UDP/TCP SOCKETS

C & JAVA EXAMPLES


Dr. Javier Alonso
http://people.duke.edu/~ja157
[email protected]
C: UPD client/server example (1/3)
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <fcntl.h>

#define SIZE 1400


char buf[SIZE];

#define TIME_PORT 13

int main(int argc, char *argv[])


{
int sockfd;
int nread;
struct sockaddr_in serv_addr,
client_addr;
int len;
C: UPD client example (2/3)
if (argc != 2) {
fprintf(stderr, “error: %s \n”, argv[0]); exit(1);
}

if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {


perror(NULL); exit(2);
}
client_addr.sin_family = AF_INET;
client_addr.sin_addr.s_addr = htonl(INADDR_ANY);
client_addr.sin_port = htons(0); In C, we need to
populate the struct
serv_addr.sin_family = AF_INET; sockaddr_in to create
serv_addr.sin_addr.s_addr = inet_addr(argv[1]); the connection between
serv_addr.sin_port = htons(TIME_PORT);
client-server or peers.
C: UPD client example (3/3)
if (bind(sockfd, (struct sockaddr *)&client_addr, sizeof(client_addr)) < 0) {
perror(NULL);
close(sockfd);
exit(3);
In C, you need to explicitly join the
} process to the port.
len = sizeof(serv_addr);
sendto(sockfd, buf, 1, 0, (struct sockaddr *)&serv_addr, len);

nread = recvfrom(sockfd, buf, SIZE, 0, (struct sockaddr *)&client_addr, &len);


write(1, buf, nread);

close(sockfd);
exit(0);
}
C: TCP client example (1/2)
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#define SIZE 1024
char buf[SIZE];
#define TIME_PORT 13

int main(argc, argv)


int argc;
char *argv[];
{
int sockfd;
int nread;
struct sockaddr_in serv_addr;
if (argc != 2) {
fprintf(stderr, "%s IPaddr\n", argv[0]);
exit(1);
}
C: TCP client example (2/2)
if ((sockfd = socket(AF_INET, SOCK_STREAM, 0))<0) {
perror(NULL);
exit(2);
}
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = inet_addr(argv[1]);
serv_addr.sin_port = htons(TIME_PORT);
if (connect(sockfd,(struct sockaddr *)&serv_addr,sizeof(serv_addr)) < 0) {
perror(NULL);
exit(3);
}
nread = read(sockfd, buf, SIZE);
write(1, buf, nread);
close(sockfd);
exit(0);
}
C: TCP Server example (1/2)
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>

int main(argc,argv)
int argc;
char *argv[];
{
int sockfd, client_sockfd;
int nread, len;
struct sockaddr_in serv_addr,
client_addr;
time_t t;
if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
perror(NULL);
exit(2);
}
C: TCP Server example (2/2)
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = INADDR_ANY;
serv_addr.sin_port = htons(TIME_PORT);
if (bind(sockfd, (struct sockaddr *)&serv_addr, sizeof(serv_addr))<0){
perror(NULL);
exit(2);
}
listen(sockfd, 5);
for (;;) {
client_sockfd = accept(sockfd,(struct sockaddr *)&client_addr, &len);
time(&t);
sprintf(buf, “%s”, asctime(localtime(t)));
len = strlen(buf) + 1;
write(client_sockfd, buf, len);
close(client_sockfd);
}
}
Java: TCP Client example (1/1)
import java.io.DataOutputStream;
import java.io.InputStreamReader;
import java.net.Socket;

public class clientJava {

public static void main(String[] args) {


Socket s = null;
try{
s = new Socket(“HOST",PORT);
DataOutputStream out = new DataOutputStream(s.getOutputStream());
in = new BufferedReader(new InputStreamReader(s.getInputStream()));
out.write(“MESSAGE");
input = in.readLine();
System.out.println(input);
}catch(Exception e){
e.printStackTrace();
}
}
}
Java: TCP Server example (1/1)
In Java, the socket creation, bind
import java.io.* ;
import java.net.* ; and listen calls are substituted by
ServerSocket and the appropriate
class Server { parameters
static final int PORT=5000;
public Server( ) {
try {
ServerSocket skServer = new ServerSocket(PORT,10);
while(true){
Socket skClient = skServidor.accept();
OutputStream aux = skClient.getOutputStream();
DataOutputStream flow= new DataOutputStream( aux );
flow.writeUTF( "Hi client ");
skClient.close();
}
} catch( Exception e ) {
System.out.println( e.getMessage() );
}
}

public static void main( String[] arg ) {


new Servidor();
}
}

You might also like