Im having trouble connecting my sockets.
On one computer I have a java server socket running
and on the other computer i have a c socket client trying to get the message
from the other computers server. But I cant get them to connect.
Do you see an error with my code?

Java Server on Computer 1

import java.net.*;
import java.io.*;

public class Server {

	public static void main(String[] args) {

		try{

			ServerSocket s = new ServerSocket(8888);
			s.setSoTimeout(10000);
			Socket v = s.accept();
			
			
			
			PrintWriter w = new PrintWriter(v.getOutputStream());
			
			w.println("This is the other computer server.");
			
			v.close();
			w.close();


		}catch(Exception e){
			System.out.println("Error");
		}

	}

}

C Client on computer 2

#include <stdio.h>
#include <unistd.h>
#include <strings.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/socket.h>




int main(void){


	int sockfd = socket(AF_INET,SOCK_STREAM,0);

	struct sockaddr_in in;

	in.sin_port = htons(8888);
	in.sin_family = AF_INET;
	in.sin_addr.s_addr = inet_addr("192.168.1.2");

	if((connect(sockfd,(struct sockaddr *)&in,sizeof(in))) !=0){
		printf("Connect Error\n");
		return 1;
	}


	char line[51];

	read(sockfd,line,sizeof(line));

	printf("%s\n",line);





	close(sockfd);




	return 0;

}

What error are you getting? Replace line 24 with

printf("connect: %s\n", strerror(errno));

Don't forget to #include <errno.h>

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.